Python regex for re-matching, storing results separately

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, , , :)

+4
3

re.findall

>>> re.findall(r'11\.\d{1,3}\.\d{1,3}\.\d{1,3}', SourceData)
['11.22.33.44', '11.11.12.11', '11.11.13.11', '11.11.14.0']
+3

, :

import re

string = """
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'
"""

rx = re.compile(r'^[^\d\n]*(11(?:\.\d+){3})', re.M)

lines = [match.group(1) for match in rx.finditer(string)]
print(lines)    

:

['11.22.33.44', '11.11.12.11', '11.11.13.11', '11.11.14.0']


^            # match start of the line
[^\d\n]*     # NOT a digit or a newline, 0+ times
11           # 11
(?:\.\d+){3} # .0-9 three times
.+           # rest of the line

re.finditer() .
. regex101.com.

+2

re.findall lookbehind, , , "ip route", :

import re
s = """
  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' 
 """
final_ips = re.findall('(?<=ip route\s)11[\d\.]+', data)

:

['11.22.33.44', '11.11.12.11', '11.11.13.11', '11.11.14.0']
+1

Source: https://habr.com/ru/post/1689647/


All Articles