如何撰寫一個 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.