Julie
Julie

Reputation: 65

Regex for IPv4 address, excluding private IPs

I need to do some data validation on a field in a piece of software which only supports validation through regular expressions. I have been searching to find a regex for what I'm looking for, however it seems all of the responses just give a better way to do it. I have to do it this way, so I'm hoping someone can help me out.

Basically, the regex should accept any valid IPv4 address, excluding the following:

From 10.0.0.0 to 10.255.255.255

From 172.16.0.0 to 172.31.255.255

From 192.168.0.0 to 192.168.255.255

I found this same question here, however nobody was actually able to answer it.

I've been using this website to try validating the regex. I started with

\b(?:(?:2(?:[0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])\.){3}(?:(?:2([0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9]))\b

which I also found on the website. This works great for IPv4 addresses, but I am now unsure how to exclude the private addresses. Does anyone have a regex for this?

Thanks.

Upvotes: 0

Views: 6963

Answers (3)

Sasikumar
Sasikumar

Reputation: 888

Just in case, If you are trying for a IP, CIDR validation (Ex: 10.123.33.12/32). try the below:

  validateCIDRAddress(data: string) {
    let isValid = false;
    const regexp = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([0-9]|[1-2][0-9]|3[0-2]))$/;
    if (data && regexp.test(data)) {
      isValid = true;
    }
    return isValid;
  }

Upvotes: 0

femtoRgon
femtoRgon

Reputation: 33341

You could add a negative lookahead checking for them ( (?!(10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.|192\.168)) ) right at the front, giving you:

\b(?!(10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.|192\.168))(?:(?:2(?:[0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])\.){3}(?:(?:2([0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9]))\b

Assuming the software you are refering to supports lookarounds, of course.

Upvotes: 4

Sam Choukri
Sam Choukri

Reputation: 1904

Regular expressions are not the best method to specify IPv4 address ranges. As you add more and more ranges, your regexp would get more and more convoluted and error-prone.

Instead, you should consider using CIDR notation.

Here are the CIDR notations for your example ranges:

From 10.0.0.0 to 10.255.255.255 -> 10.0.0.0/8
From 172.16.0.0 to 172.31.255.255 -> 172.16.0.0/12
From 192.168.0.0 to 192.168.255.255 -> 192.168.0.0/16

There is probably a library for your programming language of choice that allows you to easily determine whether a given input IPv4 address is valid for a given list of CIDR addresses.

EDIT:

I failed to notice that the OP is apparently using some sort of software that cannot validate that an IP address is within a range using any technique other than a regexp. So, if that's really the case, then obviously my alternative using CIDR notation will not work for this particular case. However, I stand by my answer that using CIDR notation to encode IP address ranges is generally a better option than using regular expressions, when CIDR notation is an option.

Upvotes: -2

Related Questions