-1

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?

Emma
  • 27,428
  • 11
  • 44
  • 69
naruto
  • 53
  • 7
  • Please formulate your example in the form of Python code, so people can run it an reproduce your result. Also, your required output only applies to the last example string, but the regex you're showing would give you a result for all four - what exactly is your question? – Grismar Aug 02 '19 at 00:22
  • i want the output with out regex group .AttributeError: 'NoneType' object has no attribute 'group' – naruto Aug 02 '19 at 00:34
  • I dont see any "IOS XR IOS Nexus Operating System IOS XE" in any of the strings – Axois Aug 02 '19 at 02:24
  • Thanks , i am able to get the required output – naruto Aug 07 '19 at 00:30

2 Answers2

2

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
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

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.

Emma
  • 27,428
  • 11
  • 44
  • 69