Advertisement

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.

The 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}|:))$
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
2001:0db8:85a3:0000:0000:8a2e:0370:7334Matchfull 8-group formTry →
2001:db8::1Matchcompressed with ::Try →
::1Matchloopback shorthandTry →
fe80::Matchtrailing compressionTry →
1:2:3:4:5:6:7:8:9No matchnine groups, no :: to compressTry →
::ffff:192.168.1.1No matchIPv4-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

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

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.

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.