fork download
  1. import re
  2. my_phrases = ['Hello world', 'apple', 'orange', 'red car']
  3. my_string = 'I am driving a red car'
  4. pattern = re.compile(r"\b(?:{})\b".format("|".join(sorted(map(re.escape, my_phrases), key=len, reverse=True))))
  5. match = pattern.search(my_string)
  6. if match:
  7. print(f"A match has been found: {match.group()}")
  8.  
  9. # => A match has been found: red car
  10.  
Success #stdin #stdout 0.03s 9464KB
stdin
Standard input is empty
stdout
A match has been found: red car