Is there a proper regex to find a string in python?

In Python, strings can be enclosed either in single quotes or double quotes:

str1 = "Hi my name is Davide"
str2 = 'Hi my name is Davide'

In each respective case, you can use the other quotes as symbols within the string:

str3 = "Hi my name is 'Davide'"
str4 = 'Hi my name is "Davide"' 

Also, in both cases, you can use the same quotes as symbols if they are escaped with a backslash:

str4 = "Hi my name is \"Davide\""
str5 = 'Hi my name is \'Davide\'

A proper way to account for all of these cases is the following regex:

(?<!\\\\)".\*?(?<!\\\\)"|(?<!\\\\)'.\*?(?<!\\\\)'

Read more here: Source link