簡體   English   中英

python regex使用findall查找多個匹配項

[英]python regex find multiple matches with findall

我需要找到2個正則表達式模式,但我沒有找到使它工作的方法

我有一個變量可能是其中之一(只是示例更多)

repository = "Red Hat Enterprise Linux Extended Update Support 7.2"

repository = "Red Hat Enterprise Linux 7"

我需要找到具有該模式的兩個存儲庫:

regex_rhel = 'Red Hat Enterprise Linux' + " " + str(major_version)

regex_eus = 'Red Hat Enterprise Linux Extended Update Support' + " " + str(rhel_ver)

我已經嘗試過findall,但得到:

eus_latest_repos = re.findall(r'"Red Hat Enterprise Linux' + " " + str(major_version)|('Red Hat Enterprise Linux Extended Update Support' + " " + str(rhel_ver),repository))
                    print(eus_latest_repos)


TypeError: unsupported operand type(s) for |: 'str' and 'tuple'

如果我了解您需要使用正則表達式從字符串中提取存儲庫。

演示:

import re
repository = "Red Hat Enterprise Linux Extended Update Support 7.2"
repository = "Red Hat Enterprise Linux 7"


s = "sadasdjkasdflsdfsdf;ljk lsjdlsdfj Red Hat Enterprise Linux Extended Update Support 7.2 kjshdklsdhksdjf sdfdfdfgdfg Red Hat Enterprise Linux 7 sdfsdfkl;dfjsgsdfg adfadsfladjksfds"


print(re.findall("Red Hat Enterprise Linux Extended Update Support\s+\d*\.?\d+|Red Hat Enterprise Linux\s+\d+", s))

輸出:

['Red Hat Enterprise Linux Extended Update Support 7.2', 'Red Hat Enterprise Linux 7']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM