Pattern library / Validation
IPv6 Address Regex (Full & Compressed)
IPv6 addresses are eight hex groups (2001:0db8:85a3:0000:0000:8a2e:0370:7334) — and real systems almost always shrink runs of zeros with :: (2001:db8::1, ::1). This pattern covers both shapes in one alternation, stays clear of lookaround, and runs unchanged in JavaScript, Python, grep and RE2 engines.
^(([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}|:))$
Load this pattern into the tester →
How the pattern works
{1,4}hex groups: one to four digits, letters a-f in either case.- Each branch after the first handles one shape of
::compression: trailing (fe80::), middle (2001:db8::1), leading (::1) — the colon-count math is exactly what the alternations encode. - Zone IDs (
fe80::1%eth0) and IPv4-mapped addresses (::ffff:192.168.1.1) are deliberately not covered — strip the suffix or match those forms with their own pattern. - Uppercase input (
2001:DB8::1) matches as-is — the hex classes accept both cases, so no flag juggling needed.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
2001:0db8:85a3:0000:0000:8a2e:0370:7334 | Match | full 8-group form | Try → |
2001:db8::1 | Match | compressed with :: | Try → |
::1 | Match | loopback shorthand | Try → |
fe80:: | Match | trailing compression | Try → |
1:2:3:4:5:6:7:8:9 | No match | nine groups, no :: to compress | Try → |
::ffff:192.168.1.1 | No match | IPv4-mapped form not covered (see pitfalls) | Try → |
Where to use it
JavaScript
const shape = /^[0-9a-fA-F:]+$/.test(v); // cheap pre-filter for logs
const strict = IPV6.test(v); // pattern from this page
// v.split(':').filter(Boolean).length tells you the group count too
Python (the right tool)
import ipaddress
ipaddress.ip_address('2001:db8::1') # parses + validates
ipaddress.ip_address('1:2:3:4:5:6:7:8:9') # raises ValueError
Bash (grep, full form only)
grep -E '^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$' ips.txt
Common mistakes
- Compression math is where lazy patterns break:
1:2:3:4:5:6:7:8:9passes any 'count the colons' check but is invalid — nine groups is not an address. - Zone identifiers and IPv4-mapped tails fail by design here; handle
%ifaceand dotted tails before matching or they will poison every comparison. - For adversarial input prefer a real parser (Python
ipaddress, Gonet.ParseIP) — regex proves shape, parsers prove semantics.
FAQ
Does this regex match ::1 and fe80::?
Yes — the leading and trailing :: compression branches are part of the alternation. Both are on this page's test table and pass.
Why does my IPv6 regex fail on compressed addresses?
Most published IPv6 regexes only cover the full 8-group form. :: deletes runs of zeros, so a pattern without compression branches rejects 2001:db8::1. Use the pattern on this page.
Is this pattern valid RE2 (GA4, BigQuery, Go)?
Yes — character classes, groups and alternation only: no lookaround, no backreferences. It runs unchanged in every RE2-based engine.
Related patterns
IPv4 Address Regex — Validate Dots & Octets · URL Regex — Match http/https Links (Practical) · Log Level Regex — Match INFO / WARN / ERROR Lines
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.