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)

Frequently asked questions

What is the regex for IPv6 address?

The pattern (?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}|::1|:: matches IPv6 address. 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.

What does this IPv6 address pattern match?

It matches strings like 2001:db8::1, fe80::1, 2001:0db8:0000:0000:0000:ff00:0042:8329, but rejects 1.2.3.4, gggg::.

How do I use this pattern in JavaScript, Python, Go, Java?

Ready-to-run snippets are provided for JavaScript, Python, Go, Java. Copy the one for your language from the Language snippets section — each wraps the same core pattern in that language's regex API.

What are common pitfalls with a IPv6 address regex?

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.

Related patterns

Try this pattern in a tool

Try any pattern live in the Regex Tester