I am a network engineer, trying to plunge into programming. I was recommended to try Python.
What I'm trying to do is save some specific data by matching a string with multiple lines with a regex. We got our data to work with saved in SourceData.
SourceData = '
ip route 22.22.22.22 255.255.255.255 TenGigabitEthernet0/1/0 1.1.1.1
ip route 33.33.33.33 255.255.255.255 TenGigabitEthernet0/1/0 1.1.1.1
ip route 11.22.33.44 255.255.255.255 TenGigabitEthernet0/1/0 1.1.1.1
ip route 11.11.12.11 255.255.255.255 TenGigabitEthernet0/1/0 1.1.1.1
ip route 11.11.13.11 255.255.255.255 TenGigabitEthernet0/1/0 1.1.1.1
ip route 11.11.14.0 255.255.255.255 TenGigabitEthernet0/1/0 1.1.1.1
ip route 44.44.44.0 255.255.255.0 TenGigabitEthernet0/1/0 1.1.1.1'
The number of rows stored in SourceDatais always unknown. There can be 0 lines (empty) to unlimited lines.
I want to match all strings containing ipv4 addresses starting with 11.
Here's what I started from the very beginning:
ip1 = re.search('11\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}', SourceData)
if ip1:
ip1 = ip1.group()
Make sure that:
>>> print ip1
11.22.33.44
OK, it seems to work. The idea is that when everything is SourceDatacompared, with the given example, the final result for this case will be 4 matches:
ip1 = 11.22.33.44
ip2 = 11.11.12.11
ip3 = 11.11.13.11
ip4 = 11.11.14.0
, , SourceData , , ? , , 4 (11.11.14.0).
Python Regex, , , :)