Regex for IPv6 Address — Pattern Explained with Examples
DodaTech
Updated Jun 20, 2026
2 min read
IPv6 address validation is increasingly important as the internet transitions from IPv4. An IPv6 address has 8 groups of 1-4 hexadecimal digits separated by colons. The :: shorthand collapses consecutive zero groups, and the last two groups may represent an embedded IPv4 address.
The Pattern
Simple full form:
/^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/Pattern Breakdown
| Part | Meaning |
|---|---|
^ | Start-of-string anchor |
([0-9a-fA-F]{1,4}:){7} | Seven groups of 1-4 hex digits each followed by a colon |
[0-9a-fA-F]{1,4} | Final eighth group of 1-4 hex digits |
$ | End-of-string anchor |
For full validation including :: shorthand and embedded IPv4, a much longer pattern is required.
Matches
- 2001:0db8:85a3:0000:0000:8a2e:0370:7334
- ::1 (loopback)
- fe80::1 (link-local)
- 2001:db8::1 (compressed)
- ::ffff:192.168.1.1 (IPv4-mapped)
Does NOT Match
- 2001:db8 (only one group)
- xyzr:1::1 (invalid hex characters)
- 2001::85a3::1 (double :: compression)
- 192.168.1.1 (plain IPv4)
- 2001:db8:85a3:0:0:8a2e:370:7334:extra (too many groups)
Language Examples
JavaScript
const ipv6Regex = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/;
console.log(ipv6Regex.test('2001:0db8:85a3:0000:0000:8a2e:0370:7334')); // true
console.log(ipv6Regex.test('192.168.1.1')); // false
Python
import re
pattern = r'^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$'
print(bool(re.match(pattern, '2001:0db8:85a3:0000:0000:8a2e:0370:7334'))) # True
print(bool(re.match(pattern, '192.168.1.1'))) # FalseRuby
pattern = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/
puts pattern.match?('2001:0db8:85a3:0000:0000:8a2e:0370:7334') # true
puts pattern.match?('192.168.1.1') # falseCommon Pitfalls
- The
::shorthand can only appear once in an address —2001::85a3::1is invalid because the expansion is ambiguous - Leading zeros in each group are optional —
2001:db8::1is the same as2001:0db8:0000:0000:0000:0000:0000:0001 - IPv4-compatible IPv6 addresses (like
::ffff:192.168.1.1) mix formats and need special handling in the pattern - Zone identifiers (e.g.
fe80::1%eth0) are valid for link-local addresses but most regexes do not include the%interfacesuffix
Real-World Use Cases
- Network configuration tools — validate IPv6 addresses entered in static network settings or DHCPv6 configurations
- Firewall rule engines — verify IPv6 addresses in security policy entries and allowlists
- DNS management systems — validate AAAA record entries before committing changes to DNS zones
FAQ
Related Patterns
Regex for IPv4 Address Regex for MAC Address
Previous
Regex for IPv4 Address — Pattern Explained with Examples
Next
Regex for MAC Address — Pattern Explained with Examples
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro