How to get the exact matching words as a list from a string with respect to a list of words in python regex

Viewed 10

I have a string

x = 'hamachi, salmon, tuna with salad, and special sauce.'

list of words

mylist = ['salmon','tuna',chicken','potato','onion','ham']

what I need as output is

['salmon','tuna']

I tried final_list = re.findall(r"(?=("+'|'.join(mylist)+r"))", x)

but the output i got is

['ham', 'salmon', 'tuna']

here ham is a wrong word as its not present in the string. How can I get the list of extract words present in string.

0 Answers
Related