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

PythonServer Side ProgrammingProgramming

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


raja
Published on 10-Jan-2018 16:41:36
Advertisements