arman_911
arman_911

Reputation: 31

modify the original list till a string is found

I am working on below output that i received from earlier config

['Te1/1/1', 'server', 'Ten', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']  
['Te1/1/2', 'desc', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']  
['Gi1/2/1', 'desc', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']  
['Gi2/1/2', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']  
['Te2/2/1', 'server', 'notconnect', '301', 'full', '10G', '10Gbase-LR']  
['Po120', 'notconnect', 'unassigned', 'auto', 'auto']  
['Po121', 'notconnect', '1', 'auto', 'auto']   

What I want is to remove any string from the list till string "connected" or "disabled" or "notconnect" is found except 1st item.

I had tried below configuration:

regex_chk = re.compile("((?:not)*?connect(?:ed)*|disabled|)")

for item in items:
    for i in item[1:]:
        if i != regex_chk:
            item.remove(i)
    print(item)

result comes as:

['Te1/1/1']
['Te1/1/2']
['Gi1/2/1']
['Gi2/1/2']
['Te2/2/1']
['Po120']
['Po121']

Whereas I want the result to be

['Te1/1/1', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']  
['Te1/1/2', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']  
['Gi1/2/1', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']  
['Gi2/1/2', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']  
['Te2/2/1', 'notconnect', '301', 'full', '10G', '10Gbase-LR']  
['Po120', 'notconnect', 'unassigned', 'auto', 'auto']  
['Po121', 'notconnect', '1', 'auto', 'auto']    

Upvotes: 3

Views: 72

Answers (4)

Ryszard Czech
Ryszard Czech

Reputation: 18631

Use a short

import re
l = ['Te1/1/1', 'server', 'Ten', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']  
regex_chk = re.compile("(?:not)?connect(?:ed)?|disabled")
print(list(filter(lambda x: not regex_chk.fullmatch(x), l)))
# ['Te1/1/1', 'server', 'Ten', 'trunk', 'full', '10G', '10Gbase-LR']

See Python proof.

filter(lambda x: not regex_chk.fullmatch(x), l) fetches all list items not fully matching the regex of yours.

Upvotes: 0

The fourth bird
The fourth bird

Reputation: 163467

The pattern ((?:not)*?connect(?:ed)*|disabled|) also matches strings like notconnected or notnotconnecteded and the | at the end makes it also match any other position.

You are also not using the regex in if i != regex_chk: It could be like if not regex_chk.match(i) instead.

But you don't need a regex for your data, you can use startswith as the words are all at the start of the string.

items = [
    ['Te1/1/1', 'server', 'Ten', 'connected', 'trunk', 'full', '10G', '10Gbase-LR'],
    ['Te1/1/2', 'desc', 'connected', 'trunk', 'full', '10G', '10Gbase-LR'],
    ['Gi1/2/1', 'desc', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver'],
    ['Gi2/1/2', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver'],
    ['Te2/2/1', 'server', 'notconnect', '301', 'full', '10G', '10Gbase-LR'],
    ['Po120', 'notconnect', 'unassigned', 'auto', 'auto'],
    ['Po121', 'notconnect', '1', 'auto', 'auto']
]

for item in items:
    for i in item[1:]:
        if not i.startswith(("notconnect", "connected", "disabled")):
            item.remove(i)
            continue
        break
    print(item)

Python demo

Output

['Te1/1/1', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']
['Te1/1/2', 'connected', 'trunk', 'full', '10G', '10Gbase-LR']
['Gi1/2/1', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']
['Gi2/1/2', 'disabled', 'routed', 'full', '1000', 'No', 'Transceiver']
['Te2/2/1', 'notconnect', '301', 'full', '10G', '10Gbase-LR']
['Po120', 'notconnect', 'unassigned', 'auto', 'auto']
['Po121', 'notconnect', '1', 'auto', 'auto']

Upvotes: 0

badhusha muhammed
badhusha muhammed

Reputation: 491

This simple pattern works for me. Try it

  for item in items:
     for i in item[1:]:
         if re.match('connected|notconnect|disabled', i):
             break
         item.remove(i)

Upvotes: 1

Minu
Minu

Reputation: 438

First, You should change the regular expression pattern.
Second, just break when you got a match.

for i in item[1:]:
    if re.match("((?:not)*?connect(?:ed)*|disabled)", i):
        break
    else:
        item.remove(i)

Upvotes: 1

Related Questions