例如:
import re pattern = r"\b(cat)\b" match = re.search(pattern, "The cat sat!") if match: print ("Match 1") match = re.search(pattern, "We s>cat 结果: >>> Match 1 Match 2 >>> "\b(cat)\b"基本匹配单词“CAT”包围的单词边界。
结果:
>>> Match 1 Match 2 >>>
0