更新时间:2022-11-14 09:53:21
你想要 Python 的 re 模块:
>>>进口重新>>>regex = re.compile(r"\sthis\s") # \s 是空格>>># 或者>>>regex = re.compile(r"\Wthis\W")>>># \w 是单词字符 ([a-zA-Z0-9_]),\W 不是单词字符>>>str2 = '研究这个'>>>str3 = '研究这个'>>>布尔(正则表达式.搜索(str2))错误的>>>regex.search(str3)<_sre.SRE_Match 对象在 0x10044e8b8>>>>布尔(正则表达式.搜索(str3))真的我有一种预感,您实际上是在寻找单词this",而不是this",并且周围有非单词字符.在这种情况下,您应该使用单词边界转义序列 \b
.
I am trying to substring search
>>>str1 = 'this'
>>>str2 = 'researching this'
>>>str3 = 'researching this '
>>>"[^a-z]"+str1+"[^a-z]" in str2
False
>>>"[^a-z]"+str1+"[^a-z]" in str3
False
I wanted to True when looking in str3. what am I doing wrong?
You want Python's re module:
>>> 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
I have a hunch you're actually looking for the word "this", not "this" with non-word characters around it. In that case, you should be using the word boundary escape sequence \b
.