I have the below text file that I would need some help with parsing out IP addresses.
The text file is of the 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, a bunch of IP networks exist as part of a log file. The output should be provided as below:
10.1.1.1/32
11.2.0.0/16
0.0.0.0/0
I have the below code but does not output the required information
file = open(filename, 'r')
for eachline in file.readlines():
ip_regex = re.findall(r'(?:\d{1,3}\.){3}\d{1,3}', eachline)
print ip_regex
In this particular case, a regex might be overkill, you could use
split
This should produce a list of strings, which are the ip addresses.
First, your regex doesn't even attempt to capture anything but four dotted numbers, so of course it's not going to match anything else, like a
/32
on the end. if you just add, e.g.,/\d{1,2}
to the end, it'll fix that:Debuggex Demo
However, if you don't understand regular expressions well enough to understand that, you probably shouldn't be using a regex as a piece of "magic" that you'll never be able to debug or extend. It's a bit more verbose with
str
methods likesplit
orfind
, but maybe easier to understand for a novice:As a side note, depending on what you're actually doing with these things, you might want to use the
ipaddress
module (or the backport on PyPI for 2.6-3.2) rather than string parsing:You can combine that with either of the above: