DevKits

Validation

Match an IPv6 address

Match IPv6 addresses in canonical or compressed form.

Pattern

Regular expression

(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}|::1|::

How it works

The pattern above matches full 8-group IPv6 plus common loopback shorthand. Full RFC 4291 conformance (all `::` compression positions) requires a much larger pattern — for real work, use an inet_pton call.

Matches

  • 2001:db8::1
  • fe80::1
  • 2001:0db8:0000:0000:0000:ff00:0042:8329
  • ::1

Non-matches

  • 1.2.3.4
  • gggg::
  • 12345::

Common gotchas

Matching every valid compressed IPv6 form with regex alone is a well-known nightmare. Use language-native parsing (`net.ParseIP` in Go, `ipaddress` in Python) when correctness matters.

Language snippets

JavaScript

const re = /(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}|::1|::/g;

Python

import ipaddress
try: ipaddress.IPv6Address("2001:db8::1")
except ValueError: pass

Go

import "net"
addr := net.ParseIP("2001:db8::1")
if addr != nil && addr.To4() == nil { /* v6 */ }

Java

Pattern.compile("(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}|::1|::").matcher(text)

Related patterns

Try any pattern live in the Regex Tester