I need to find all occurrences of a list of words in a text using regex. For example, given the words:
words = {'i', 'me', 'my'}
and some
text = 'A book is on the table. I have a book on the table. My book is on the table. There is my book on the table.'
should return result = ["I", "My", "my"]
I'm using this:
re.findall(r"'|'.join(words))", text,flags=re.IGNORECASE))
But it's returning an empty list.
Also if I use this:
re.findall(r"(?=("+'|'.join(words)+r"))", text, flags=re.IGNORECASE))
returns:
['i', 'I', 'My', 'i', 'i', 'my']
which is incorrect.