\ b在python的重新和精确的单词匹配

时间:2016-11-26 04:43:20

标签: python regex string

如何使\ b正确尊重字边界?例如,了解'而不是部分匹配...

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

1 个答案:

答案 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