user3806770 user3806770 - 1 year ago 103
Python Question

\b in python's re and exact word match

How to make \b properly respect the word boundary? For example, understand the ' and not does partial match...

>>> import re
>>> str = "This is a test's test"
>>> p1 = r'\b' + 'test' + r'\b'
>>> re.findall(p1,str)
['test', 'test']

Answer Source

Using negative look-ahead assertion, you can assure matching test that is not followed by '.

>>> import re
>>> s = "This is a test's test"
>>> re.findall(r"\btest\b(?!')", s)  # match `test` as long as it is not followed by "'"

BTW, do not use str as a variable name. It shadows built-in function/type str.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download