fork download
  1. import re
  2. words = ["THIS", "THAT"]
  3. sentences = ["I want to identify THIS word.", "And THAT!", "But I do not want to identify !THIS word", "Or [THIS] word"]
  4.  
  5. pattern = fr"(?<!\S)(?:{'|'.join(words)})\b"
  6. for sentence in sentences:
  7. word_re = re.search(pattern, sentence)
  8. if word_re:
  9. print(f"'{sentence}' is a match!")
Success #stdin #stdout 0.02s 9500KB
stdin
Standard input is empty
stdout
'I want to identify THIS word.' is a match!
'And THAT!' is a match!