Evgen Stoyka Evgen Stoyka - 16 days ago 7
Python Question

Python | Strange string compare with regex

When i'm trying to compare matched slice of string by regex it gives me not valid result. Where it can be wrong?

>>> import re
>>> m = re.match(r"([\d\w]{2})([\d\w]{2})", "7L7M")
>>> m.group(1)
'7L'
>>> m.group(1) is not '7L'
True
>>> '7L' is not '7L'
False
>>> type(m.group(1))
<class 'str'>`

Answer

You must compare strings using the equality operator ==, not the identity operator is

>>> m.group(1) == '7L'
True