简体   繁体   中英

Regex errors during python loop

I have the following strings:

String-1:

Cisco IOS XR Software, Version 5.3.4[Default]

String-2:

Cisco IOS Software, C3900 Software (C3900-UNIVERSALK9-M), Version 15.4(3)M3, RELEASE SOFTWARE (fc2)

String-3:

Cisco Nexus Operating System (NX-OS) Software

String-4:

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)

When I run the following regex, I will get the output, but sometimes it fails with the following error:

AttributeError: 'NoneType' object has no attribute 'group'

Regex used:

re.compile(r'(Cisco(.*)Software)')
re.search(regex_version,session)

Regex used:

re.compile(r'(Cisco(.*)Software)')
re.search(regex_version,session)

Required output:

IOS XR
IOS
Nexus Operating System
IOS XE

How do I solve the problem?

Using re.search will:

Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding match object.

You get that error message if you try use access a group which does not exists. To prevent that, you could check if there is a match object.

To get your desired values, you might use a single capturing group with a character class [A-Za-z ] to specify what you would allow to match and a tempered greedy token approach :

\bCisco\s+((?:(?!\bSoftware\b)[A-Za-z ])*)\s.*?Software

Regex demo | Python demo

For example

import re

regex = r"\bCisco\s+((?:(?!\bSoftware\b)[A-Za-z ])*)\s.*?Software"

strings = [
    "Cisco IOS XR Software, Version 5.3.4[Default]",
    "Cisco IOS Software, C3900 Software (C3900-UNIVERSALK9-M), Version 15.4(3)M3, RELEASE SOFTWARE (fc2)",
    "Cisco Nexus Operating System (NX-OS) Software",
    """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)"""
]

for s in strings:
    matches = re.search(regex, s)
    if matches:
        print(matches.group(1))

Result

IOS XR
IOS
Nexus Operating System
IOS XE

I'm guessing that this expression is likely to return the desired output:

\bCisco\s+(.*?)\s+Software\b

Test

import re

regex = r"\bCisco\s+(.*?)\s+Software\b"

test_str = """
Cisco IOS XR Software, Version 5.3.4[Default]
Cisco IOS Software, C3900 Software (C3900-UNIVERSALK9-M), Version 15.4(3)M3, RELEASE SOFTWARE (fc2)
Cisco Nexus Operating System (NX-OS) Software
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)
"""
print(re.findall(regex, test_str))

Output

['IOS XR', 'IOS', 'Nexus Operating System (NX-OS)', 'IOS XE', 'IOS']

The expression is explained on the top right panel of regex101.com , if you wish to explore/simplify/modify it, and in this link , you can watch how it would match against some sample inputs, if you like.

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