简体   繁体   中英

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

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.

I tried ths code:

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

it gives output:

15.5

03.08.07.E

expected output:

15.5(1)SY2

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]+).+

Explanation:

Version - matches Version literally

\s+ - matches one or more of white spaces

([^,\s]+) - match one or more characters other from whitespace \s or comma , and store it inside first capturing group

.+ - 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

Demo

I think the best is;

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

example output:

6.4.2

03.06.06E

03.16.07b.S

12.0(18b)

12.2(33.3.8)SB13

12.2(33)SXI2a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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