devkult_
tools26converters34
home/regex/ipv6

Regex for IPv6 Address Validation

A full IPv6 address is eight groups of 1–4 hex digits separated by colons, and that's what this pattern validates. The honest caveat: IPv6's :: compression (dropping runs of zero groups) makes a complete regex enormous and easy to get subtly wrong — if you need to accept ::1 or 2001:db8::7334, expand the address first or use a real parser.

/^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/

How it works, token by token

TokenMeaning
^(start of string, open the repeating group
[0-9a-fA-F]{1,4}one group: 1 to 4 hex digits (leading zeros optional)
:){7}a colon after the group — seven times
[0-9a-fA-F]{1,4}$the eighth group, then end of string

What it matches

2001:0db8:85a3:0000:0000:8a2e:0370:7334
fe80:0:0:0:0:0:0:1
2001:db8:85a3:0:0:8a2e:370:7334
::1
2001:db8
g001:db8:85a3:0:0:8a2e:370:7334

Try it live

One candidate per line — the m flag makes the ^ and $ anchors apply to each line. Edit anything; it runs in your browser.

/

Variations

^::1$|^::$just the loopback and unspecified addresses — the two compressed forms worth special-casing
^([0-9a-fA-F]{1,4}:){1,7}:$addresses ending in :: (trailing zero groups compressed)

Language notes

  • Prefer a parser for anything user-facing: net.ParseIP in Go, ipaddress.IPv6Address in Python, and the URL host parser in browsers all handle compression correctly.
  • The complete compressed-form regex runs to hundreds of characters with eight alternations — it exists, but nobody can review it.

Frequently asked questions

Why doesn't this match ::1?

::1 uses zero compression — the :: stands in for seven zero groups. Supporting every compressed position roughly squares the pattern's complexity, which is why this page validates full form and recommends expanding or parsing compressed input.

How do I normalize an IPv6 address to full form?

Use the standard library: ipaddress.IPv6Address(s).exploded in Python gives the full eight-group form; most languages have an equivalent. Then this pattern (or simple string logic) applies cleanly.

Related patterns

Building something custom? The regex tester gives you live match highlighting for any pattern.