wishi
wishi

Reputation: 7387

What is the correct way to parse IPs and CIDRs out of text in Python

I found many threads about this topic, with distinct and complex answers.

For IPv4:

ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}', file)

That works for 8.8.8.8 but not for 8.8.8.8/32. I tried to add some (?:/(\d+) to catch the /X. Without success.

Now I also see IPv6 IPs in my file. Do you really have to regex-search for IPv6 IPs the same way? Is there some easier way to search for IPs in text with Python?

Upvotes: 0

Views: 48

Answers (1)

cs95
cs95

Reputation: 402263

You can use this pattern (for IPv4):

[0-9]+(?:\.[0-9]+){3}(?:\/[\d]+)?

Which matches the optional subnet when present. Here's a demo.

For matching in python, you'll need to do this:

ip = re.findall(r'\b[0-9]+(?:\.[0-9]+){3}(?:\/[\d]+)?\b', file, flags=re.M)

Assuming, file is a multiline string. If not, you'll need to do file.read().

Upvotes: 1

Related Questions