Write 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']


Updated on: 2020-02-20T05:31:13+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements