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


Updated on: 20-Feb-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements