Mark Wei
Mark Wei

Reputation: 101

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

Upvotes: 0

Views: 117

Answers (5)

Mark Wei
Mark Wei

Reputation: 101

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.

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174874

Through regex module,

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

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89639

You can use the new regex module:

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)

Upvotes: 1

Abhijit
Abhijit

Reputation: 63787

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

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

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 118021

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

Upvotes: 0

Related Questions