python正则表达式与findall找到多个匹配项

时间:2018-05-03 11:30:22

标签: python

我需要找到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'

1 个答案:

答案 0 :(得分:0)

如果我理解你需要一个正则表达式从字符串中提取repo。

<强>演示:

(onAppLoad)="window.hideAppLoader($event)"

<强>输出:

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))