简体   繁体   English

使用正则表达式 python 从 output 中提取版本号?

[英]Extract the Version number from the output using regex python?

I want the version number from the output and for both the output I want one code only我想要 output 和 output 的版本号,我只想要一个代码

Edition: Read the output starts with Cisco till Version then extract the Version Number版本:阅读 output 从 Cisco 开始直到版本然后提取版本号

For example:Read the line like this Cisco IOS Software, s2t54 Software (s2t54-ADVIPSERVICESK9-M), Version 15.5(1)SY2, the ouput Version例如:读这样一行 Cisco IOS Software, s2t54 Software (s2t54-ADVIPSERVICESK9-M), Version 15.5(1)SY2, 输出版本

Output: 15.5(1)SY2 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 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,版本:2.2,(0x80E80022)核心:E500,版本: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. 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.在 GPL 2.0 版下许可的软件代码是根据 GPL 2.0 版条款获得 GPL 代码的免费软件。

I tried ths code:我试过这个代码:

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

it gives output:它给出了 output:

15.5 15.5

03.08.07.E 03.08.07.E

expected output:预期 output:

15.5(1)SY2 15.5(1)SY2

03.08.07.E 03.08.07.E

I think this is the expression you are looking for:我认为这是您正在寻找的表达方式:

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

It matches anything after "Version" until it finds a comma or a blank space它匹配“版本”之后的任何内容,直到找到逗号或空格

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

Explanation:解释:

Version - matches Version literally Version - 从字面上匹配Version

\s+ - matches one or more of white spaces \s+ - 匹配一个或多个空格

([^,\s]+) - match one or more characters other from whitespace \s or comma , and store it inside first capturing group ([^,\s]+) - 匹配除空格\s或逗号之外的一个或多个字符,并将其存储在第一个捕获组中

.+ - match one or more of any characters (except newline), this is to consume rest of the output and prevent from matching multiple versions in one output .+ - 匹配任意字符中的一个或多个(换行符除外),这是为了消耗output的rest,防止在一个output中匹配多个版本

Demo演示

I think the best is;我认为最好的是;

a = r'Version\s*(.+?)[\s|,|[]' a = r'版本\s*(.+?)[\s|,|[]'

example output:例如 output:

6.4.2 6.4.2

03.06.06E 03.06.06E

03.16.07b.S 03.16.07b.S

12.0(18b) 12.0(18b)

12.2(33.3.8)SB13 12.2(33.3.8)SB13

12.2(33)SXI2a 12.2(33)SXI2a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM