match IPs more robustly and multiple per line

This commit is contained in:
Connor Olding 2020-09-04 14:48:23 +02:00
parent 70308230e6
commit 921c51e118

View file

@ -1,17 +1,24 @@
import re
ipv4_pattern = re.compile(r"(\d+)\.(\d+)\.(\d+)\.(\d+)", re.ASCII)
ipv4_pattern_strict = re.compile(r"""
(?:^|(?<=[^\d.]))
(0|1\d\d|2[0-4]\d|25[0-5]|[1-9]\d?)
\.
(0|1\d\d|2[0-4]\d|25[0-5]|[1-9]\d?)
\.
(0|1\d\d|2[0-4]\d|25[0-5]|[1-9]\d?)
\.
(0|1\d\d|2[0-4]\d|25[0-5]|[1-9]\d?)
(?:$|(?=[^\d.]))
""", re.ASCII | re.VERBOSE)
def read_ips(f):
# TODO: make more robust. (regex pls)
for ip in f.readlines():
if "#" in ip:
ip, _, _ = ip.partition("#")
ip = ip.strip()
if ip.count(".") != 3:
continue
yield ip
for line in f.readlines():
line, _, _ = line.partition("#") # ignore comments
for match in ipv4_pattern_strict.finditer(line):
yield match.group() # yield the entire string
def addr_to_int(ip):