python regex使用find查找多个匹配项

2024-06-02 07:14:31 发布

您现在位置: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)

我试过芬德尔,但得到:

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'

Tags: extendedsupportversionrepositorylinuxhat模式update
1条回答
网友
1楼 · 发布于 2024-06-02 07:14:31

如果我知道你需要正则表达式从字符串中提取回购。你知道吗

演示:

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

相关问题 更多 >