[英]\b in python's re and exact word match
如何使\\ b正確遵守單詞邊界? 例如,了解'而不是部分匹配...
>>> import re
>>> str = "This is a test's test"
>>> p1 = r'\b' + 'test' + r'\b'
>>> re.findall(p1,str)
['test', 'test']
使用否定的前瞻性斷言 ,可以確保沒有跟隨'
匹配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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.