Skip to content

Regex for Ipv6 — Pattern, Examples & Caveats

Pattern
^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|::)$

This pattern covers the standard IPv6 textual forms: the full eight-group address, every placement of a single :: compression run, and the unspecified address ::. It is a practical subset rather than an exhaustive RFC 4291 grammar with embedded IPv4. Validation patterns answer a yes/no question about an entire input: does this string conform to the expected format? They are anchored with ^ and $ so the whole value must match, and they belong at the edge of your system — form fields, API payloads and import files — where rejecting bad input early prevents downstream errors. A validation regex is a contract check, not a transformation. It needs no special flags. Apply it to the whole input as a format contract. It does not handle IPv4-mapped addresses like ::ffff:192.168.1.1 or zone identifiers (fe80::1%eth0); extend or use a library for those. Multiple :: runs are invalid in IPv6 and this pattern rejects them, but exhaustive validation is complex — prefer a real parser for security-sensitive checks. Hex digits are case-insensitive here via the a-fA-F class rather than the i flag, keeping the pattern self-contained. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.

Advertisement
Ad slot · ref-top

Token-by-token explanation

  • ([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4} — full form: eight hex groups
  • ([0-9a-fA-F]{1,4}:){1,7}: — compression at the end (trailing ::)
  • ([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4} — one group after a middle ::
  • ([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2} — two groups after ::
  • further branches — three to six groups after ::, and leading :: forms
  • :((:[0-9a-fA-F]{1,4}){1,7}|:) and :: — leading-compression and unspecified address

What it matches

2001:0db8:85a3:0000:0000:8a2e:0370:73342001:db8::1::1::fe80::

What it does not match

2001:db8:::112345::2001:db8:85a3gggg::1

When to use it

Reach for a validation pattern when data crosses a trust boundary: a signup form, an API request body, a CSV import or a configuration value. Run it before any processing so malformed input is rejected with a clear message instead of causing a cryptic failure later. Keep the pattern in one shared place, document the exact format it accepts, and pair it with a real parser when the value has semantics (dates, numbers, addresses) that a shape check alone cannot confirm.

Caveats

  • It does not handle IPv4-mapped addresses like ::ffff:192.168.1.1 or zone identifiers (fe80::1%eth0); extend or use a library for those.
  • Multiple :: runs are invalid in IPv6 and this pattern rejects them, but exhaustive validation is complex — prefer a real parser for security-sensitive checks.
  • Hex digits are case-insensitive here via the a-fA-F class rather than the i flag, keeping the pattern self-contained.

Code examples

JavaScript
const re = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|::)$/;
const input = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
if (re.test(input)) {
  // valid: proceed
} else {
  // reject with a helpful message
}
Python
import re

rx = re.compile(r"^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|::)$")
value = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
if rx.fullmatch(value):
    pass  # valid
else:
    raise ValueError("invalid format")
Java
import java.util.regex.*;

Pattern p = Pattern.compile("^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|::)$");
Matcher m = p.matcher("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
boolean ok = m.matches();   // whole input must conform
PHP
<?php
$subject = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
if (preg_match('/^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|::)$/', $subject, $m)) {
    // $m[0] is the full match
}

How to test it

Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.

Frequently asked questions

Why is full IPv6 validation so hard?

RFC 4291 allows many compression placements, embedded IPv4, and zone ids. A single regex becomes huge and error-prone, so libraries like Python's ipaddress are the robust choice.

Does it accept ::1 (loopback)?

Yes — the leading-:: branches match a :: followed by a single group, so ::1 is accepted, as is the unspecified address ::.

Should validation regexes be anchored?

Yes — use ^ and $ (or fullmatch) so the entire input must conform. Without anchors, a valid-looking substring inside garbage would pass, letting malformed data through your validation layer.

What is catastrophic backtracking and how do I avoid it?

Nested quantifiers like (a+)+ can explode exponentially on non-matching input, freezing the engine (ReDoS). Avoid ambiguous nesting, prefer atomic groups or possessive quantifiers where supported, and bound input length.

Advertisement
Ad slot · ref-in-article

Try the Regex Tester

Live regex matching with groups, flags and explanations.

Open tool →