查找与字符串完全匹配的字符串 [英] finding an exact match for string
问题描述
我使用以下函数查找字符串中单词的完全匹配.
I used the following function to find the exact match for words in a string.
def exact_Match(str1, word):
result = re.findall('\\b'+word+'\\b', str1, flags=re.IGNORECASE)
if len(result)>0:
return True
else:
return False
exact_Match(str1, word)
但是当"award"和"award-winning"这两个词仅应在以下字符串中获奖时,我得到一个完全匹配的词.
But I get an exact match for both words "award" and "award-winning" when it only should be award-winning for the following string.
str1 = "award-winning blueberries"
word1 = "award"
word2 = "award-winning"
我如何才能使re.findall将整个单词与连字符和其他标点符号匹配?
How can i get it such that re.findall will match whole words with hyphens and other punctuations?
推荐答案
设置您自己的单词边界:
Make your own word-boundary:
def exact_Match(phrase, word):
b = r'(\s|^|$)'
res = re.match(b + word + b, phrase, flags=re.IGNORECASE)
return bool(res)
从此处复制粘贴到我的解释器:
copy-paste from here to my interpreter:
>>> str1 = "award-winning blueberries"
>>> word1 = "award"
>>> word2 = "award-winning"
>>> exact_Match(str1, word1)
False
>>> exact_Match(str1, word2)
True
实际上,强制转换为bool
是不必要的,根本没有帮助.没有它,功能会更好:
Actually, the casting to bool
is unnecessary and not helping at all. The function is better off without it:
def exact_Match(phrase, word):
b = r'(\s|^|$)'
return re.match(b + word + b, phrase, flags=re.IGNORECASE)
注意:exact_Match
是非常非常规的大小写.只需将其称为精确匹配"即可.
note: exact_Match
is pretty unconventional casing. just call it exact_match.
这篇关于查找与字符串完全匹配的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!