Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在正则表达式中查找重复模式_Python_Regex - Fatal编程技术网

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

Python 如何在正则表达式中查找重复模式,python,regex,Python,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'''.spl

我试图从字符串列表中找到正则表达式模式。特别是试图返回端口

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')
使用这段代码,我可以筛选出第一个字符串,但我需要其余的

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

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

这不是使用正则表达式,但它应该可以做到这一点

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]
>>> 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']
输出

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

如果您想使用正则表达式路径,下面的正则表达式模式就可以了

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]
>>> 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']
regex的另一个替代方法是使用

您可以使用:


通过
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']
导入正则表达式 >>>数据=''348 VLAN0348活动Fa4/24、Fa4/26、Gi2/4 ... 349 VLAN0349活动Fa6/40、Fa6/43、Fa6/45 ... 350 VLAN0350有源Fa6/41,Gi3/40'''
>>>m=regex.findall(r’(?我想我是通过筛选“/”得到答案的

没有大多数答案那么复杂,谢谢你!我将仔细看看正则表达式模块

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)