I want to extract a network group from a Cisco config using Python and regular expressions.
The group starts with object-group network Cloudflare
and just after that, there are one or more subnets like network-object 173.245.48.0 255.255.240.0
.
The text would be
object-group network Cloudflare
network-object 173.245.48.0 255.255.240.0
network-object 103.21.244.0 255.255.252.0
network-object 103.22.200.0 255.255.252.0
network-object 103.31.4.0 255.255.252.0
network-object 141.101.64.0 255.255.192.0
network-object 108.162.192.0 255.255.192.0
network-object 190.93.240.0 255.255.240.0
network-object 188.114.96.0 255.255.240.0
network-object 197.234.240.0 255.255.252.0
network-object 198.41.128.0 255.255.128.0
network-object 162.158.0.0 255.254.0.0
network-object 104.16.0.0 255.248.0.0
network-object 104.24.0.0 255.252.0.0
network-object 172.64.0.0 255.248.0.0
network-object 131.0.72.0 255.255.252.0
While using this regex (?P<name>object-group network Cloudflare)\n(?P<subnets> network-object \d+\.\d+\.\d+\.\d+ \d+\.\d+\.\d+\.\d+\n)*
I get 3 match groups, of which only the 1st is usable but it contains the header object-group ...
.
Can it be improved?
https://regex101.com/r/s925cq/1