使用regex python从输出中提取版本号?

2024-06-12 08:09:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要输出的版本号,对于这两个输出,我只想要一个代码

Edition: Read the output starts with Cisco till Version then extract the Version Number

For example:Read the line like this Cisco IOS Software, s2t54 Software (s2t54-ADVIPSERVICESK9-M), Version 15.5(1)SY2, the ouput Version

Output : 15.5(1)SY2

Output 1: ''' Cisco IOS Software, s2t54 Software (s2t54-ADVIPSERVICESK9-M), Version 15.5(1)SY2, RELEASE SOFTWARE (fc6) ROM: System Bootstrap, Version 12.2(50r)SYS3, RELEASE SOFTWARE (fc1) CPU: MPC8572_E, Version: 2.2, (0x80E80022) CORE: E500, Version: 3.0, (0x80210030) '''

Output 2: Cisco IOS Software, IOS-XE Software, Catalyst 4500 L3 Switch Software (cat4500es8-UNIVERSALK9-M), Version 03.08.07.E RELEASE SOFTWARE (fc2) 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.

我试过密码:

r = re.findall(r'Version\s*(([\w]+))', str)
r[0]

它提供输出:

15.5

03.08.07.E

预期产量:

15.5(1)SY2

03.08.07.E


Tags: thereadoutputreleaseversionsoftwaregplcisco
2条回答

我想这就是你想要的表达方式:

Version\s*(.+?)[\s|,]

它匹配“Version”之后的任何内容,直到找到逗号或空格

试试Version\s+([^,\s]+).+

说明:

Version-按字面意思匹配Version

\s+-匹配一个或多个空格

([^,\s]+)-匹配除空格\s或逗号,之外的一个或多个字符,并将其存储在第一个捕获组中

.+-匹配任何字符中的一个或多个(除换行符外),这将消耗剩余的输出并防止在一个输出中匹配多个版本

Demo

相关问题 更多 >