How can I match an exact word in a string?
Question:
I have a string in which the word local
occurs many times. I used the find()
function to search for this word, but it will also find e.g. locally
. How can I match local
exactly?
Answers:
Look for ‘ local ‘? Notice that Python is case sensitive.
For this kind of thing, regexps are very useful :
import re
print(re.findall('\blocal\b', "Hello, locally local test local."))
// ['local', 'local']
b means word boundary, basically. Can be space, punctuation, etc.
Edit for comment :
print(re.sub('\blocal\b', '*****', "Hello, LOCAL locally local test local.", flags=re.IGNORECASE))
// Hello, ***** locally ***** test *****.
You can remove flags=re.IGNORECASE if you don’t want to ignore the case, obviously.
Do a regular expression search for blocalb
b is a “word boundry” it can include beginnings of lines, ends of lines, punctuation, etc.
You can also search case insensitively.
You could use regular expressions to constrain the matches to occur at the word boundary, like this:
import re
p = re.compile(r'blocalb')
p.search("locally") # no match
p.search("local") # match
p.findall("rty local local k") # returns ['local', 'local']
Below you can use simple function.
def find_word(text, search):
result = re.findall('\b'+search+'\b', text, flags=re.IGNORECASE)
if len(result)>0:
return True
else:
return False
Using:
text = "Hello, LOCAL locally local test local."
search = "local"
if find_word(text, search):
print "i Got it..."
else:
print ":("
line1 = "This guy is local"
line2 = "He lives locally"
if "local" in line1.split():
print "Local in line1"
if "local" in line2.split():
print "Local in line2"
Only line1 will match.
Using Pyparsing:
import pyparsing as pp
def search_exact_word_in_string(phrase, text):
rule = pp.ZeroOrMore(pp.Keyword(phrase)) # pp.Keyword() is case sensitive
for t, s, e in rule.scanString(text):
if t:
return t
return False
text = "Local locally locale"
search = "Local"
print(search_exact_word_in_string(search, text))
Which Yields:
['Local']
quote = "No good deed will go unrewarded"
location = quote.rfind("go")
print(location)
// use rfind()
If you want to check for existence, try to think the other way…
and you can make something like this…
check for a pattern not equal to what you want exactly and if there’s a match
then check if the result is equal to what you want.
st1:str ='local!'
st2:str =' locally!'
match1 = re.search(r'localw?',st1)
match2 = re.search(r'localw?',st2)
print('yes' if match1 and match1.group()=='local' else 'no')
print('yes' if match2 and match2.group()=='local' else 'no')
I have a string in which the word local
occurs many times. I used the find()
function to search for this word, but it will also find e.g. locally
. How can I match local
exactly?
Look for ‘ local ‘? Notice that Python is case sensitive.
For this kind of thing, regexps are very useful :
import re
print(re.findall('\blocal\b', "Hello, locally local test local."))
// ['local', 'local']
b means word boundary, basically. Can be space, punctuation, etc.
Edit for comment :
print(re.sub('\blocal\b', '*****', "Hello, LOCAL locally local test local.", flags=re.IGNORECASE))
// Hello, ***** locally ***** test *****.
You can remove flags=re.IGNORECASE if you don’t want to ignore the case, obviously.
Do a regular expression search for blocalb
b is a “word boundry” it can include beginnings of lines, ends of lines, punctuation, etc.
You can also search case insensitively.
You could use regular expressions to constrain the matches to occur at the word boundary, like this:
import re
p = re.compile(r'blocalb')
p.search("locally") # no match
p.search("local") # match
p.findall("rty local local k") # returns ['local', 'local']
Below you can use simple function.
def find_word(text, search):
result = re.findall('\b'+search+'\b', text, flags=re.IGNORECASE)
if len(result)>0:
return True
else:
return False
Using:
text = "Hello, LOCAL locally local test local."
search = "local"
if find_word(text, search):
print "i Got it..."
else:
print ":("
line1 = "This guy is local"
line2 = "He lives locally"
if "local" in line1.split():
print "Local in line1"
if "local" in line2.split():
print "Local in line2"
Only line1 will match.
Using Pyparsing:
import pyparsing as pp
def search_exact_word_in_string(phrase, text):
rule = pp.ZeroOrMore(pp.Keyword(phrase)) # pp.Keyword() is case sensitive
for t, s, e in rule.scanString(text):
if t:
return t
return False
text = "Local locally locale"
search = "Local"
print(search_exact_word_in_string(search, text))
Which Yields:
['Local']
quote = "No good deed will go unrewarded"
location = quote.rfind("go")
print(location)
// use rfind()
If you want to check for existence, try to think the other way…
and you can make something like this…
check for a pattern not equal to what you want exactly and if there’s a match
then check if the result is equal to what you want.
st1:str ='local!'
st2:str =' locally!'
match1 = re.search(r'localw?',st1)
match2 = re.search(r'localw?',st2)
print('yes' if match1 and match1.group()=='local' else 'no')
print('yes' if match2 and match2.group()=='local' else 'no')