0

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

5 Answers 5

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.