Categories
ipv6 networking regex

Regular expression that matches valid IPv6 addresses

111

I’m having trouble writing a regular expression that matches valid IPv6 addresses, including those in their compressed form (with :: or leading zeros omitted from each byte pair).

Can someone suggest a regular expression that would fulfill the requirement?

I’m considering expanding each byte pair and matching the result with a simpler regex.

2

56

The following will validate IPv4, IPv6 (full and compressed), and IPv6v4 (full and compressed) addresses:

'/^(?>(?>([a-f0-9]{1,4})(?>:(?1)){7}|(?!(?:.*[a-f0-9](?>:|$)){8,})((?1)(?>:(?1)){0,6})?::(?2)?)|(?>(?>(?1)(?>:(?1)){5}:|(?!(?:.*[a-f0-9]:){6,})(?3)?::(?>((?1)(?>:(?1)){0,4}):)?)?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?4)){3}))$/iD'

9

  • 8

    Even though validating ip-s could be done as Frank Krueger suggests, this solution is the one that actually answers the question (though I haven’t fully tested it yet) as well as if you have many IP-s that you want syntactically test and maybe match for in a line of text, you can’t use the IP validation technique.

    – Gyuri

    Dec 6, 2011 at 0:33

  • Hi, I tested this RegExp and don’t worked for me. It says D is an invalid flag and when I remove it it says “SyntaxError: invalid quantifier”

    – Diosney

    Dec 4, 2012 at 6:37

  • 3

    JavaScript implements a subset of Perl-style regular expressions, not the entirety of PCRE. My regex won’t work without some of the advanced features of PCRE.

    Dec 4, 2012 at 18:29

  • 2

    This gives exception for me in C#

    – sarat

    May 7, 2013 at 5:01

  • 2

    Failing test case: FE80:0000:0000:0000:0202:B3FF:FE1E:8329 Using latest version of Elixir on this date, which uses PCRE underneath.

    – pmarreck

    Oct 20, 2017 at 16:23


26

It sounds like you may be using Python. If so, you can use something like this:

import socket

def check_ipv6(n):
    try:
        socket.inet_pton(socket.AF_INET6, n)
        return True
    except socket.error:
        return False

print check_ipv6('::1') # True
print check_ipv6('foo') # False
print check_ipv6(5)     # TypeError exception
print check_ipv6(None)  # TypeError exception

I don’t think you have to have IPv6 compiled in to Python to get inet_pton, which can also parse IPv4 addresses if you pass in socket.AF_INET as the first parameter. Note: this may not work on non-Unix systems.

5

  • 4

    You should specify the exception type in the except clause. Otherwise, except will catch everything and may mask unrelated errors. The type here should be socket.error.

    Dec 20, 2009 at 14:54

  • A) inet_pton doesn’t throw other exceptions, unless the docs are wrong, and B) even if it did, what else would you return but False?

    Dec 22, 2009 at 0:35

  • 2

    Re: other errors… if the user passes in a non-string, TypeError gets eaten. Clearly a list isn’t an ipv6, but I’d probably want to have it carp that I was passing in the wrong type.

    Apr 21, 2010 at 20:44

  • 1

    +1 This helped me a lot. A couple of additional points that should be added: 1) socket.inet_pton can be used to test the validity of both families of IP addresses (IP and IPv6). 2) The docs here (docs.python.org/2/library/socket.html) suggest that this is available on Unix platforms. It might not be available on Win-platforms.

    Dec 5, 2013 at 9:13

  • using django and this helps!

    May 18, 2015 at 11:24