python - 正则表达式从字符串中过滤版本
问题描述
我有以下字符串:
字符串 1-
Cisco IOS Software, C3900 Software (C3900-UNIVERSALK9-M), Version 15.4(3)M3, RELEASE SOFTWARE (fc2) ROM: System Bootstrap, Version 15.0(1r)M16, RELEASE SOFTWARE (fc1)
字符串2-
Cisco IOS XE Software, Version 16.05.01b
Cisco IOS Software [Everest], ISR Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.5.1b, RELEASE SOFTWARE (fc1)
licensed under the GNU General Public License ("GPL") Version 2.0. The
software code licensed under GPL Version 2.0 is free software that comes
GPL code under the terms of GPL Version 2.0. For more details, see the
从我只需要获取的字符串16.05.01b
和15.4(3)M3
运行正则表达式时获取。
我已经尝试过了,r'((?<=Version\s)\d+\.\d+\(\d+...)'
我无法15.4(3)M3
获取16.05.01b
。
和r'((?<=Version\s)\d+\.\d+\(\d+...)'
一个正则表达式应该能够从两个字符串中获取版本,但两者都不给我结果。
解决方案
在您的示例中,版本带有前缀Version
并包括:
- 数字
- 点
- 括号
- 人物
在这里,我将版本建模为以数字开头并以上述项目的组合继续的东西。
这应该有效:
import re
strings = [
'-M), Version 15.4(3)M3, RELEA',
'rap, Version 15.0(1r)M16, RELEA',
', Version 16.5.1b, RELEASE',
're, Version 16.05.01b'
]
version_re = re.compile(r'version (\d[\w.()]+)', flags=re.IGNORECASE)
for s in strings:
v = version_re.search(s).group(1)
print(v)
输出:
15.4(3)M3
15.0(1r)M16
16.5.1b
16.05.01b
推荐阅读
- laravel - Laravel 默认身份验证使用 Microsoft Graph 登录或创建用户
- javascript - 当我从 Typescript 中的 NPM 包中导入类型时,它来自哪个文件?
- html - 如何从视频中删除视频下载选项?
- python - 如何重复函数和处理程序,直到用户对电报机器人给出有效回复?
- python - Archlinux / Python / Qt | 不能同时使用 OpenCV 和 matplotlib
- azure - Azure Web App - Kudu github 部署不起作用
- html - CSS应用于错误的对象Angular 8
- java - org.springframework.dao.InvalidDataAccessApiUsageException:参数值 [false] 与预期类型不匹配 [java.lang.Boolean (n/a)];
- android - Android Studio 将数据从 ListView 传递到 AlertDialog
- python - Matplotlib 运行并引发错误 (MatplotlibDeprecationWarning)