如何撰写一个 Python 正则表达式来匹配任意位置的多个词?


以下使用 Python 正则表达式的代码匹配指定字符串中的给定多个单词

示例

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)

输出

这给出了输出

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


更新于: 20-Feb-2020

3K+ 浏览

开启你的 职业生涯

完成课程获取认证

开始
广告
© . All rights reserved.