I have a list of words and a string. I want to take each word in the list of words and check if it matches any of the words in the string.
wordList = ['i', 'love', 'this', 'phone', 'amazing', 'be']
string = "love amazing great best good nice"
I have the following code:
for word in wordsList:
posMatch = re.search(word, string)
if posMatch:
print (posMatch.group())
So in this case, the output is:
i
love
amazing
be
But I need the output to be:
love
amazing
It is taking "i" and the "be" as a match because they are parts of some of the words in the string. I could place a space before and after the regex, but I am not sure how to do that. Anyone have a good way to do this? Thanks in advance.