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')]
re.findall(r'(?sm)^IP = (\S+).*?^State = (\S+)',output)if your input is well formatted and there is aStatefor eachIP.re.DOTALL+re.MULTILINE