如何使\ b正确尊重字边界?例如,了解'而不是部分匹配...
>>> import re
>>> str = "This is a test's test"
>>> p1 = r'\b' + 'test' + r'\b'
>>> re.findall(p1,str)
['test', 'test']
答案 0 :(得分:4)
使用negative look-ahead assertion,您可以确保匹配test
后面没有'
。
>>> 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']
顺便说一句,不要使用str
作为变量名。它隐藏了内置函数/类型str
。