正则表达式从字符串中过滤版本

时间:2019-07-18 12:38:39

标签: python regex python-regex

我有以下字符串:

字符串1-

Cisco IOS Software, C3900 Software (C3900-UNIVERSALK9-M), Version 15.4(3)M3, RELEASE SOFTWARE (fc2) ROM: System Bootstrap, Version 15.0(1r)M16, RELEASE SOFTWARE (fc1)

字符串2

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)
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.  For more details, see the
从运行这两个字符串的

运行Regex时,我只需要获得16.05.01b和15.4(3)M3即可。

尝试过此r'((?<=Version\s)\d+\.\d+\(\d+...)' 我能够获取15.4(3)M3而不是16.05.01b。

r'((?<=Version\s)\d+\.\d+\(\d+...)'

一个正则表达式应该能够从两个字符串中获取版本,但是两个都不给我结果。

请帮助。谢谢

3 个答案:

答案 0 :(得分:3)

在您的示例中,版本的前缀为Version,其中包括:

  • 数字
  • 括号
  • 字符

在这里,我将版本建模为以数字开头,并以上述各项的组合继续。

这应该有效:

import re
strings = [
    '-M), Version 15.4(3)M3, RELEA',
    'rap, Version 15.0(1r)M16, RELEA',
    ', Version 16.5.1b, RELEASE',
    're, Version 16.05.01b'
]
version_re = re.compile(r'version (\d[\w.()]+)', flags=re.IGNORECASE)
for s in strings:
    v = version_re.search(s).group(1)
    print(v)

输出:

15.4(3)M3
15.0(1r)M16
16.5.1b
16.05.01b

答案 1 :(得分:0)

好吧,因为您的正则表达式希望在搜索版本时找到括号,而第二个字符串中没有该括号。

这是解决它的简单方法(从abdusco借来了字符串):

strings = [
    '-M), Version 15.4(3)M3, RELEA',
    'rap, Version 15.0(1r)M16, RELEA',
    ', Version 16.5.1b, RELEASE']

    versions = []
    version = re.compile(r'(?<=Version\s)\d+\.\d........')
        for s in strings:
            v = version.search(s).group(0).split(',')[0]
            version.append(v)

答案 2 :(得分:0)

您可以使用交替方式获得两个值。

您也可以省略捕获组,因为它是唯一匹配开头或结尾括号的匹配项,后跟A-Z和一个数字,或者匹配一个点,两个数字和一个字符a-z

(?<=Version\s)\d+\.\d+(?:\([^()+]\)[A-Z]\d|\.\d{2}[a-z])

Regex demo | Python demo

更有效的版本可能是使用捕获组,而不是后面的样子:

Version\s(\d+\.\d+(?:\([^()+]\)[A-Z]\d|\.\d{2}[a-z]))

Regex demo

import re

regex = r"(?<=Version\s)\d+\.\d+(?:\([^()+]\)[A-Z]\d|\.\d{2}[a-z])"

test_str = ("String 1-Cisco IOS Software, C3900 Software (C3900-UNIVERSALK9-M), Version 15.4(3)M3, RELEASE SOFTWARE (fc2)\n"
    "ROM: System Bootstrap, Version 15.0(1r)M16, RELEASE SOFTWARE (fc1)\n\n"
    "String2-Cisco IOS XE Software, Version 16.05.01b\n"
    "Cisco IOS Software [Everest], ISR Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.5.1b, RELEASE SOFTWARE (fc1)\n"
    "licensed under the GNU General Public License (\"GPL\") Version 2.0.  The\n"
    "software code licensed under GPL Version 2.0 is free software that comes\n"
    "GPL code under the terms of GPL Version 2.0.  For more details, see the")

print (re.findall(regex, test_str))

结果

['15.4(3)M3', '16.05.01b']