-1

I'm using Python 3.9. I want to determine if a word from an array is in another string. I have

words = ['i', 'we', 'my', 'our', 'mine']

so this

title1 = "My mind"

should contain at least one word (the word "my"), but when I tried to write a regex it failed ...

>>> result = any(re.search(r'\b' + word + '\b',title1.lower()).group(0) for word in words)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <genexpr>
AttributeError: 'NoneType' object has no attribute 'group'
satish
  • 703
  • 5
  • 23
  • 52

1 Answers1

0

You can use any to check if at least a word is part of the sentence.

>>> any(re.search(rf"\b{word}\b", title1, re.IGNORECASE) for word in words)
True
abc
  • 11,579
  • 2
  • 26
  • 51