>>> import re
>>> regex = re.compile(r"\sthis\s") # \s is whitespace
>>> # OR
>>> regex = re.compile(r"\Wthis\W")
>>> # \w is a word character ([a-zA-Z0-9_]), \W is anything but a word character
>>> str2 = 'researching this'
>>> str3 = 'researching this '
>>> bool(regex.search(str2))
False
>>> regex.search(str3)
<_sre.SRE_Match object at 0x10044e8b8>
>>> bool(regex.search(str3))
True
对于Python中的正则表达式,请使用^{} 模块:
您需要Python的re模块:
我有预感你实际上是在找“这个”这个词,而不是“这个”周围有非单词字符。在这种情况下,应该使用单词边界转义序列^{} 。
看起来您想要使用正则表达式,但是您使用的是普通的字符串方法。您需要使用^{} module 中的方法:
相关问题 更多 >
编程相关推荐