Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading