简体   繁体   中英

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

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 .

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>

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

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()

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
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!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM