简体   繁体   English

如何在正则表达式中查找重复模式

[英]How to find recurring patterns in regex

I'm trying to find a regex pattern from list of strings. 我正在尝试从字符串列表中找到一个正则表达式模式。 Specifically trying to return the ports. 专门尝试返回端口。

data = '''348  VLAN0348                         active    Fa4/24, Fa4/26, Gi2/4
349  VLAN0349                         active    Fa6/40, Fa6/43, Fa6/45
350  VLAN0350                         active    Fa6/41, Gi3/40'''.split('\n')

Using this code I've been able sift out the first string, but I need the rest. 使用此代码,我已经能够筛选出第一个字符串,但我需要其余的字符串。

FoundPorts = []
if checkVlan in x:
        port = re.search(r'active    (\S*)',x)
            if port != None:
                FoundPorts.append(port.group(1))`

Ideally I'd get: 理想情况下,我会得到:

FoundPorts = ['Fa4/24','Fa4/26','Gi2/4','Fa6/40','Fa6/43','Fa6/45','Fa6/41','Gi3/40']

You can use the new regex module : 您可以使用新的regex模块

import regex

data = '''348  VLAN0348                         active    Fa4/24, Fa4/26, Gi2/4
349  VLAN0349                         active    Fa6/40, Fa6/43, Fa6/45
350  VLAN0350                         active    Fa6/41, Gi3/40'''

print regex.findall(r'(?:\G(?!^),\s*|\bactive)\s+([^\s,]+)', data)

This isn't using a regex, but it should do the trick 这不是使用正则表达式,但应该可以解决问题

data = '''348  VLAN0348                         active    Fa4/24, Fa4/26, Gi2/4
349  VLAN0349                         active    Fa6/40, Fa6/43, Fa6/45
350  VLAN0350                         active    Fa6/41, Gi3/40'''

[i.strip(",") for i in data.split() if "/" in i]

Output 产量

['Fa4/24', 'Fa4/26', 'Gi2/4', 'Fa6/40', 'Fa6/43', 'Fa6/45', 'Fa6/41', 'Gi3/40']

If you want to do with the regex path, the following regex pattern would do the trick 如果要使用正则表达式路径,则以下正则表达式模式可以解决问题

>>> re.findall(r"[A-Z][a-z]\d/\d\d?", data)
['Fa4/24', 'Fa4/26', 'Gi2/4', 'Fa6/40', 'Fa6/43', 'Fa6/45', 'Fa6/41', 'Gi3/40']

Another alternative to regex, that will work with your sample data is to use fnmatch 正则表达式的另一种替代方法(适用于您的示例数据)是使用fnmatch

>>> import fnmatch
>>> [elem.rstrip(',') for elem in fnmatch.filter(data.split(),"*/*")]
['Fa4/24', 'Fa4/26', 'Gi2/4', 'Fa6/40', 'Fa6/43', 'Fa6/45', 'Fa6/41', 'Gi3/40']

Through regex module, 通过regex模块,

>>> import regex
>>> data = '''348  VLAN0348                         active    Fa4/24, Fa4/26, Gi2/4
... 349  VLAN0349                         active    Fa6/40, Fa6/43, Fa6/45
... 350  VLAN0350                         active    Fa6/41, Gi3/40'''
>>> m = regex.findall(r'(?<=active\s*)[\w/]+|(?<=,\s*)[\w/]+', data)
>>> m
['Fa4/24', 'Fa4/26', 'Gi2/4', 'Fa6/40', 'Fa6/43', 'Fa6/45', 'Fa6/41', 'Gi3/40']

I think i got the answer by sifting for "/". 我认为我通过筛选“ /”得到了答案。

for x in d:
    s = re.findall(r'(\S*/\S*)',x)
    if s != None:
        print s
        if len(s) > 1:
            FoundPorts .extend(s)
        elif len(s) == 1:
            FoundPorts .extend(s)

Not as complex as most of the answers and thank you for them! 不像大多数答案那样复杂,谢谢您! I'll take a closer look at the Regex module. 我将仔细研究Regex模块。

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

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