3

RFC1918 defines private IPv4 addresses as those that fall within any of the following ranges:

10.0.0.0 - 10.255.255.255
172.16.0.0 - 172.31.255.255
192.168.0.0 - 192.168.255.255

I'm adding 127.0.0.1 to this list, for the purposes of my analysis. I know there are tried-and tested regex's to match any IPv4 address, but how would I narrow one of these to down to matching only if the address falls in one of the above ranges or in 127.0.0.1? Will be using Python.

Many thanks

Pyderman
  • 14,809
  • 13
  • 61
  • 106
  • 1
    I don't know how you do this with a regex. I think you'll need to convert the addresses to variables then see if the value being passed in falls in between any of the ranges in your list. http://stackoverflow.com/questions/9590965/convert-an-ip-string-to-a-number-and-vice-versa – chris85 Jun 05 '15 at 20:07
  • 1
    Don't make the mistake of believing that only 127.0.0.1 is loopback. 127.0.0.0/8 is, in fact, a class A address, meaning that anything that begins with 127 will map to loopback. For your purposes I think this will matter. – David Hoelzer Jun 05 '15 at 20:12

3 Answers3

7

In Python 3.3+ (you did not specify a version, nor why regular expressions are a requirement, so I’ll put this here for completeness), you can:

import ipaddress

addr_string = # string with your IP address
try:
    addr = ipaddress.IPv4Address(addr_string)
except ValueError:
    raise # not an IP address
if addr.is_private:
    pass # is a private address

See also: ipaddress module documentation

Jonas Schäfer
  • 20,140
  • 5
  • 55
  • 69
4

The following regexp should work:

^(?:10|127|172\.(?:1[6-9]|2[0-9]|3[01])|192\.168)\..*

DEMO

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    This unfortunately is not the correct answer. It also matches this address: 192.168.266.333 and even a full match on this total syntax error: 10.333.333.333. with the period trailing. – Collin Chaffin Jul 04 '17 at 03:42
  • 1
    @CollinChaffin The way I understood the question is that he has something that's already a valid IP address, and he's just trying to classify it as being a private versus public IP. This isn't being used to find the IP addresses in the first place. – Barmar Jul 04 '17 at 05:53
  • I am working on implementing your solution, and just for sanity check I am including this example - 10.256.255.255. Your regex flags this as private given the ranges, but the regex should not be capturing this number, as it is outside the range of 10.0.0.0 – 10.255.255.255. Do you think your regex maybe incomplete or am I misunderstanding something? I am new to regex, hence the question. – Arsik36 Jun 30 '21 at 16:10
0
(?:(?:192\.)(?:(?:168\.)(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.)(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))|(?:(?:10\.)(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|(?:(?:172\.)(?:(?:1[6-9]|2[0-9]|3[0-1])\.)(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.)(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))

This regex works for fetching all private ips from a string. Demo can be found here

thatjoe
  • 1
  • 1