Evgen Stoyka Evgen Stoyka - 1 year ago 120
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'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download