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']
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 "'"
['test']
BTW, do not use str
as a variable name. It shadows built-in function/type str
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.