Regular Expression for IPv6 addresses

Phil Pennock phil.pennock at spodhuis.org
Sat Feb 20 01:08:50 UTC 2010


On 2010-02-04 at 17:50 -0500, Richard E. Brown wrote:
> My company, Dartware, have derived a regex for testing whether an IPv6 address  
> is correct. I've posted it in my blog:
> 
> 	http://intermapper.ning.com/profiles/blogs/a-regular-expression-for-ipv6
> 
> This has links to the regular expression, a (Perl) program that tests various  
> correct and malformed addresses, and a Ruby implementation of the same.

There's a full grammar in RFC 3986 (URI Generic Syntax) already, which
can be translated straight.  It too handles the embedded IPv4 addresses.

While your code is written in a more condensed manner, those who want to
be able to cross-check against the RFC might want to take a look at this
one, which emits a PCRE regexp:
  http://people.spodhuis.org/phil.pennock/software/emit_ipv6_regexp-0.304
  http://people.spodhuis.org/phil.pennock/software/emit_ipv6_regexp-0.304.asc

(Version numbers for repository, not for that one script :) ).

FWIW, the ability to grab a shell variable which contains an RE for IPv6
addresses, which can be used in:
  pcregrep "$ipv6_regex" log_file
has proven very useful, especially when debugging newly-added IPv6
support for an app.  This is also the most coherent justification I've
come up with so far for using a regexp instead of a dedicated parser,
other than "because I could".

Regards,
-Phil




More information about the NANOG mailing list