Home > OS >  Select String after string with regex in python
Select String after string with regex in python

Time:12-18

Imagine that we have a string like:

Routing for Networks:
0.0.0.0/32
5.6.4.3/24
2.3.1.4/32
Routing Information Sources:
Gateway         Distance      Last Update
192.168.61.100        90      00:33:51
192.168.61.103        90      00:33:43
Irregular IPs:
1.2.3.4/24
5.4.3.3/24

I need to get a list of IPs between "Routing for Networks:" and "Routing Information Sources:" like below:

['0.0.0.0/32","5.6.4.3/24","2.3.1.4/32"]

What I have done till now is:

Routing for Networks:\n(. (?:\n. )*)\nRouting

But it is not working as expected.

UPDATE: my code is as bellow:

re.findall("Routing for Networks:\n(. (?:\n. )*)\nRouting", string)

CodePudding user response:

The value of capture group 1 included the newlines. You can split the value of capture group 1 on a newline to get the separated values.

If you want to use re.findall, you will a list of group 1 values, and you can split every value in the list on a newline.

An example with a single group 1 match:

import re

pattern = r"Routing for Networks:\n(. (?:\n. )*)\nRouting"

s = ("Routing for Networks:\n"
            "0.0.0.0/32\n"
            "5.6.4.3/24\n"
            "2.3.1.4/32\n"
            "Routing Information Sources:\n"
            "Gateway         Distance      Last Update\n"
            "192.168.61.100        90      00:33:51\n"
            "192.168.61.103        90      00:33:43")

m = re.search(pattern, s)
if m:
    print(m.group(1).split("\n"))

Output

['0.0.0.0/32', '5.6.4.3/24', '2.3.1.4/32']

For a bit more precise match, and if there can be multiple of the same consecutive parts, you can match the format and use an assertion for Routing instead of a match:

Routing for Networks:\n((?:(?:\d{1,3}\.){3}\d{1,3}/\d \n) )(?=Routing)

Example

pattern = r"Routing for Networks:\n((?:(?:\d{1,3}\.){3}\d{1,3}/\d \n) )(?=Routing)"
s = "..."
m = re.search(pattern, s)
if m:
    print([s for s in m.group(1).split("\n") if s])

See a regex demo and a Python demo.

  • Related