Search code examples
pythonregexstringstring-matchingexact-match

Python: How to determine if a string has an exact match with any string from the list


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


Solution

  • 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. Note re.search returns None if no match is found.

    import re
    
    phrases = ['hello', 'hi', 'bye']
    def match(text):
      r = re.search(r'\b({})\b'.format("|".join(phrases)), text)
      return r is not None
    
    match("hi there, how are you?"), match("hithere, how are you?")
    # (True, False)