Regular Expression for IPv6 addresses

Richard E. Brown Richard.E.Brown at DARTWARE.COM
Sat Feb 6 22:16:54 UTC 2010


Thanks for all the comments on the IPv6 regex...

>  Jeroen Massar <jeroen at> wrote:
>  The only proper way of "testing" if an address is a valid IPv6 address
>  is to feed it to getaddrinfo() and then use it through that API.

Good point. One of the reasons to do this was for environments where getaddrinfo()  
might not be availble. (For example, in a Javascript - see the page on the InterMapper  
site: )

>  Of course, there should be only limited places where a user can enter or
>  see IP addresses in the first place. There is this great thing called
>  DNS which is what most people should be using.

Another good point. But look at Seiichi's note (below)...

>  Seiichi Kawamura <kawamucho at> also> wrote:

>  This might be of some interest to you.

We believe this correctly recognizes all the cases specified by RFC4291. But it's  
a great idea to update the Javascript page above to reformat to this recommendation.

>  Carsten Bormann <cabo at>
>  I was looking at this regexp for my Ruby course as a great example of "how not
>  to use Regexps".
>  But thank you for this textbook example!
>  And, of course, an error did creep in.

You're quite welcome! :-)

But seriously... Do you have an example of an address that causes the RE to fail?

>  If you really need an RE, the right approach here is to write a small program  
>  generate the RE.

Absolutely. The Perl program cited includes code much like your example.

Rich Brown                    richard.e.brown at
Dartware, LLC       
66-7 Benning Street           Telephone: 603-643-9600
West Lebanon, NH 03784-3407   Fax: 603-643-2289

PS Dartware is sponsoring a table displaying our InterMapper network monitoring  
software at the NANOG48 Beer 'n Gear. Please come by to introduce yourself and  
take a look...

More information about the NANOG mailing list