import re
regex = re.compile(r"addr:([\d.]+)", flags=re.MULTILINE)
test_str = ("eth0 \n"
"Link encap:Ethernet HWaddr xx:xx:xx:xx:xx:xx\n"
"inet addr:10.20.30.40 Bcast:10.20.30.254 Mask:255.255.255.0\n"
"UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1\n"
"RX packets:66196498 errors:0 dropped:32831 overruns:0 frame:0\n"
"TX packets:61850152 errors:0 dropped:0 overruns:0 carrier:0\n"
"collisions:0 txqueuelen:1000\n"
"RX bytes:41122659013 (41.1 GB) TX bytes:28800593238 (28.8 GB)\n"
"Interrupt:22 Memory:f6ae0000-f6b00000")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html