python重新查找以#开头的单词

时间:2019-06-11 21:00:38

标签: python regex

我正在尝试使用python re findall或finditer方法来匹配单词。

import re

re.compile(r"\bSOMETHING\b").findall('this is the SOMETHING i am looking for')
>>> ['SOMETHING'] # i expect this outcome

re.compile(r"\bSOMETHING\b").findall('this is the SOMETHINGELSE i am looking for')
>>> [] # i expect this outcome

>>> re.compile(r"\bSOMETHING\b").findall('this is the #SOMETHING i am looking for')
['SOMETHING'] # i don't expect this outcome but []

>>> re.compile(r"\b#SOMETHING\b").findall('this is the #SOMETHING i am looking for')
[] # i expect this outcome but ['#SOMETHING']

我想我不明白为什么加#会弄乱整个查找过程。我不确定如何匹配包含#或任何其他特殊字符的模式。

谢谢

0 个答案:

没有答案