Assume that I have the list of phrases to compare against as: ["hello", "hi", "bye"]
I want to return true if my text has any of this words in it, but with exact match. Meaning that: hi there, how are you? returns true, but hithere, how are you? returns false.
So far I have the below code:
phrases = ['hello', 'hi', 'bye']
def match(text: str) -> bool:
if any(ext in text for ext in phrases):
return True
else:
return False
But it returns true for both inputs.
I also found out about this below function which returns the exact matches from the string, but I want to compare against a list of phrases, not a single string. I know I can iterate through the list of words and check one by one, but hoping to find a solution that is better performing.
import re
print(re.findall('\\bhello\\b', "hellothere, how are you?"))
Update: By exact match, I mean word boundary. That can be space, punctuation, etc. Just like what \b is
A regex of the form
r"(abc|ef|xxx)"will match with "abc", "ef", or "xxx". You can create this regex by using the string concatenation as below. Notere.searchreturns None if no match is found.