Jegor Jegor - 1 month ago 6
Python Question

Python: regex parse text to create dict

I have a problem with one task:

I have output from cisco hw.

IP access list 100
10 permit igmp any any
20 deny any any
IP access list 200
10 permit ip 192.168.1.1/32
20 permit ip 192.168.2.1/32 any
30 permit ip 192.168.3.3/32 any
40 deny any any


The task is to make a dict with access list number as key and access list rule number as value.

acl_dict = {'100' : '10', '100' : '20','200': '10', '200': '20', '200': '30', '200': '40'}

I have written a regex:

rx = re.compile("""
list\s(.*)[\n\r]
\s{4}(\d{1,3}).+$
""",re.MULTILINE|re.VERBOSE)
for match in rx.finditer(text):
print (match.group(1))
print (match.group(2))


But is shows only number from first two strings (100 and 10)
I need to modify somehow regex to match all numbers to make needed dict.
Can anyone help ?

Answer

It's possible to do it with a single method by using the newest regex module:

import regex

text = """
IP access list 100  
    10 permit igmp any any  
    20 deny any any  
IP access list 200  
    10 permit ip 192.168.1.1/32   
    20 permit ip 192.168.2.1/32 any  
    30 permit ip 192.168.3.3/32 any  
    40 deny any any
"""

acl_dict = {}
rx = regex.compile("list\s(.+)[\n\r](\s{4}(\d{1,3}).+[\n\r])*", regex.MULTILINE|regex.VERBOSE)
for match in rx.finditer(text):
    acl_dict[match.group(1)] = match.captures(3)

print(acl_dict)

Output:

$ python3 match.py 
{'200  ': ['10', '20', '30', '40'], '100  ': ['10', '20']}
Comments