簡體   English   中英

\\ b在python中與精確詞匹配

[英]\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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM