[122697] in North American Network Operators' Group

home help back first fref pref prev next nref lref last post

Re: Regular Expression for IPv6 addresses

daemon@ATHENA.MIT.EDU (Phil Pennock)
Fri Feb 19 20:09:33 2010

Date: Sat, 20 Feb 2010 02:08:50 +0100
From: Phil Pennock <phil.pennock@spodhuis.org>
To: "Richard E. Brown" <Richard.E.Brown@dartware.com>
Mail-Followup-To: "Richard E. Brown" <Richard.E.Brown@dartware.com>,
	nanog@nanog.org
In-Reply-To: <4912284@blitz.dartware.com>
Cc: nanog@nanog.org
Errors-To: nanog-bounces+nanog.discuss=bloom-picayune.mit.edu@nanog.org

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


home help back first fref pref prev next nref lref last post