Ad

Python Regexp Not Working With String Containing Apostrophes

- 1 answer

So I have a string that looks like this:

"greeting = 'hello;'"

and I try to match this regexp to it:

"**'.*;.\*'"**

so that it detects that there is a ; char inside of two apostrophes on both sides. When I simply try this out on regex101.com it works, but when I copy paste this to python it doesn't -

g = re.match("'**.\*;.\***'", "greeting = 'hello;'") 

returns None

but simply putting '**.\*;.***' into regex on regex101 and greeting = 'hello;' into the text field there works (matches 'hello;' as intended). How is this possible? How to fix this, so that it is correctly detected in Python also?

Ad

Answer

re.match has to match from the beginning of the string. re.search will match anywhere in the string.

Ad
source: stackoverflow.com
Ad