regex - IP address/network parsing from text file using python -
i have below text file need help parsing out ip addresses.
the text file of form
abc 10.1.1.1/32 aabbcc def 11.2.0.0/16 eeffgg efg 0.0.0.0/0 ddeeff
in other words, bunch of ip networks exist part of log file. output should provided below:
10.1.1.1/32 11.2.0.0/16 0.0.0.0/0
i have below code not output required information
file = open(filename, 'r') eachline in file.readlines(): ip_regex = re.findall(r'(?:\d{1,3}\.){3}\d{1,3}', eachline) print ip_regex
first, regex doesn't effort capture 4 dotted numbers, of course of study it's not going match else, /32
on end. if add, e.g., /\d{1,2}
end, it'll prepare that:
(?:\d{1,3}\.){3}\d{1,3}/\d{1,2}
debuggex demo
however, if don't understand regular expressions plenty understand that, shouldn't using regex piece of "magic" you'll never able debug or extend. it's bit more verbose str
methods split
or find
, maybe easier understand novice:
for line in file: part in line.split() try: address, network = part.split('/') a, b, c, d = address.split('.') except valueerror: pass # not in right format else: # part, or address , network, or whatever
as side note, depending on you're doing these things, might want utilize ipaddress
module (or the backport on pypi 2.6-3.2) rather string parsing:
>>> import ipaddress >>> s = '10.1.1.1/32' >>> = ipaddress.ip_network('10.1.1.1/32')
you can combine either of above:
for line in file: part in line.split(): try: = ipaddress.ip_network(part) except valueerror: pass # not right format else: # , nifty methods
python regex ip
No comments:
Post a Comment