简体   繁体   English

python循环期间的正则表达式错误

[英]Regex errors during python loop

I have the following strings: 我有以下字符串:

String-1: 字串1:

Cisco IOS XR Software, Version 5.3.4[Default]

String-2: 字串2:

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

String-3: 字串3:

Cisco Nexus Operating System (NX-OS) Software

String-4: 字符串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' AttributeError:'NoneType'对象没有属性'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: 使用re.search将:

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 : 为了获得所需的值,您可以使用一个字符类别为[A-Za-z ]的单个捕获组来指定允许匹配的对象,并使用一种温和的贪婪令牌方法

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

Regex demo | 正则表达式演示 | Python demo Python演示

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. regex101.com右上角的面板上说明了该表达式,如果您希望对其进行探索/简化/修改,并且在此链接中 ,您可以根据需要观看它与某些示例输入的匹配方式。

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

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