'Regex to extract a data in multi line string containing sections
I have a below multi line string which has various section,
sample='''
GigabetI/0/0/1 is up, line protocol is up
keep alive set
internet address 10.10.10.2
LoopBack0 is up, line protocol is up
keep alive is not set
internet address 10.10.10.3
Tunnel12 is up, line protocol is up
keep alive is set
internet address 10.10.10.12
TenGigabetI/0/0/2 is up, line protoco is up
keep alive is set
internet addresss is 192.12.168.1 '''
my expected output will be a list of dictionary
d=[{'interface':'GigabetI/0/0/1','IP':'10.10.10.2'},{'interface':'LoopBack0','IP':'10.10.10.3'},{'interface':'Tunnel12 ','IP':'10.10.10.12'},{'interface':'TenGigabetI/0/0/2 ','IP':'192.12.168.1'}]
I need a separate dictionary for each section, but i failed to extract data in this section output
Solution 1:[1]
Here is one way to do this using re.finditer with named capture groups:
import re
sample = "GigabetI/0/0/1 is up, line protocol is up keep alive set internet address 10.10.10.2 LoopBack0 is up, line protocol is up keep alive is not set internet address 10.10.10.3 Tunnel12 is up, line protocol is up keep alive is set internet address 10.10.10.12 TenGigabetI/0/0/2 is up, line protoco is up keep alive is set internet addresss is 192.12.168.1"
r = re.compile('(?P<interface>\S+).*?(?P<IP>\d(?:\.\d+){3})')
d = [m.groupdict() for m in r.finditer(sample)]
print(d)
This prints:
[{'interface': 'GigabetI/0/0/1', 'IP': '0.10.10.2'},
{'interface': 'LoopBack0', 'IP': '0.10.10.3'},
{'interface': 'Tunnel12', 'IP': '0.10.10.12'},
{'interface': 'TenGigabetI/0/0/2', 'IP': '2.12.168.1'}]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Biegeleisen |
