如何在 Python 中编写正则表达式以匹配任意位置的多个单词?


以下代码使用 Python regex 匹配给定字符串中的给定多个单词

示例

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


更新时间: 2020 年 2 月 20 日

3K+ 浏览量

开启你的 职业生涯

完成课程以获得认证

开始
广告
© . All rights reserved.