Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python regexp not working with string containing apostrophes

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 –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading