How to search for a word (exact match) within a string?

亡梦爱人 提交于 2019-12-22 20:43:53

问题


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?


回答1:


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.




回答2:


It looks like you want to use regular expressions, but you are using ordinary string methods. You need to use the methods in the re module:

import re
>>> re.search("[^a-z]"+str1+"[^a-z]", str2)
>>> re.search("[^a-z]"+str1+"[^a-z]", str3)
<_sre.SRE_Match object at 0x0000000006C69370>



回答3:


For regular expressions in Python, use the re module:

>>> import re
>>> re.search("[^a-z]"+str1+"[^a-z]", str2) is not None
False
>>> re.search("[^a-z]"+str1+"[^a-z]", str3) is not None
True



回答4:


import re
str1 = 'this'
str2 = 'researching this'
str3 = 'researching this '

if re.search("[^a-z]"+str1+"[^a-z]", str2):
    print "found!"

if re.search("[^a-z]"+str1+"[^a-z]", str3):
    print "found!"



回答5:


I don't think in does a regex search.

Take a look at the re module.

It's unclear what you're actually trying to do, but if you want to know if "this" is in "researching this", do:

"this" in "researching this"

(or)

str1 in str3

Or if you're trying to find it as an entire word only, do:

"this" in "researching this".split()

The result is that it will split "researching this" into ["researching", "this"] and then check for the exact word "this" in it. So, this is False:

"this" in "researching thistles".split()



回答6:


use re module. re module is the one that you should use. re rocks.



来源:https://stackoverflow.com/questions/7641779/how-to-search-for-a-word-exact-match-within-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!