python - python 正则表达式使用 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'
解决方案
如果我理解你需要一个正则表达式来从字符串中提取 repo。
演示:
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']
推荐阅读
- dart - 在 Flutter 中,Dependencies 必须指定版本号?
- log4j - 删除 Log4j 并使用 Logback
- vba - 根据条件创建新的单元格值
- db2 - db2 v7R1 - 触发器 INSERT New As XML Str
- mysql - MySQL:仅当另一列中的值唯一时才设置值
- php - 如何在 Firebird 中将 BLOB SUB_TYPE 0 转换为 TEXT?
- python - 在 pandas groupby DataFrame 中创建异常值列
- spring - Spring Websocket:如何通过任何 convertAndSend 或 convertAndSendToUser 方法拦截 SimpMessagingTemplate 发送的消息?
- xamarin - 使用 msbuild 命令行 xcarchive 到 ipa
- python - 以编程方式运行 Ansible 剧本?