I am new to Python regex. I trying to get below lst from the output.
output="""
IP = 10.10.10.1
BGP version 4, remote router ID 0.0.0.0
State = Established
IP = 10.10.10.2
BGP version 4, remote router ID 0.0.0.0
State = Active
IP = 10.10.10.3
BGP version 4, remote router ID 0.0.0.0
State = Active
IP = 10.10.10.4
BGP version 4, remote router ID 0.0.0.0
State = Established
"""
I am trying with below regex but no luck. Can someone please help me.
lst = re.findall(r'IP = (\S+)\n\nState = (\S+)',output, re.M)
lst
should be filled with
[('10.10.10.1', 'Established'), ('10.10.10.2', 'Active'), ('10.10.10.3', 'Active'), ('10.10.10.4', 'Established')]
Try this:
In [101]: pat = r'IP\s*\=\s*([^\n\r]*)[\r\n][^\=]*?State\s*\=\s*([^\n\r]*)'
In [102]: re.findall(pat, output, flags=re.M & re.S)
Out[102]:
[('10.10.10.1', 'Established'),
('10.10.10.2', 'Active'),
('10.10.10.3', 'Active'),
('10.10.10.4', 'Established')]
NOTE: please pay attention at @WiktorStribiżew's RegEx , which is much more elegant:
re.findall(r'(?sm)^IP = (\S+).*?^State = (\S+)',output)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.