Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python re.match doesnt match the same regexp

Tags:

python

regex

I'm facing a weird problem; I hope nobody asked this question before I need to match two regexp containing "(" ")".

Here is the kind of tests I made to see why it's not working:

>>> import re
>>> re.match("a","a")
<_sre.SRE_Match object at 0xb7467218>

>>> re.match(re.escape("a"),re.escape("a"))
<_sre.SRE_Match object at 0xb7467410>

>>> re.escape("a(b)")
'a\\(b\\)'

>>> re.match(re.escape("a(b)"),re.escape("a(b)"))

=> No match

Can someone explain me why the regexp doesn't match itself ?

Thanks a lot


1 Answers

You've escaped special characters, so your regex will match the string "a(b)", not the string
'a\(b\)' which is the result of re.escape('a(b)').

like image 181
mgilson Avatar answered May 26 '26 09:05

mgilson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!