Evgen Stoyka Evgen Stoyka - 10 months ago 82
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)
>>> m.group(1) is not '7L'
>>> '7L' is not '7L'
>>> type(m.group(1))
<class 'str'>`

Answer Source

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

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