如何在 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.