How to write a Python regular expression to match multiple words anywhere?



The following code using Python regex matches the given multiple words in the given string

Example

import re
s = "These are roses and lilies and orchids, but not marigolds or .."
r = re.compile(r'\broses\b | \bmarigolds\b | \borchids\b', flags=re.I | re.X)
print r.findall(s)

Output

This gives the output

['roses', 'orchids', 'marigolds']



Advertisements