regex pattern · ready to copy
Regex for matching IPv6 addresses
Match standard IPv6 + the `::` shorthand. Compromise between strict RFC-5952 and practical.
intermediate
javascript / pcre / python4 use cases
The pattern
(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}|(?:[A-Fa-f0-9]{1,4}:){1,7}:|::(?:[A-Fa-f0-9]{1,4}:){0,6}[A-Fa-f0-9]{1,4}|::
Test cases
| Input | Result |
|---|---|
| 2001:0db8:85a3:0000:0000:8a2e:0370:7334 | ✓matches |
| fe80::1 | ✓matches |
| ::1 | ✓matches |
| 2001:db8::ff00:42:8329 | ✓matches |
| 192.168.1.1 (IPv4) | ✗rejects |
| g001::1 (g not hex) | ✗rejects |
| :::1 | ✗rejects |
Edge cases & caveats
Embedded IPv4 (e.g., `::ffff:192.0.2.1`) needs alternation extension. Strict RFC-5952 lowercase-only canonical form requires post-match validation.
Common use cases
- server log scraping
- container network audit
- IPv6 transition planning
- load-balancer config parsing
Try variations against your data
regexlab is a free in-browser tester with side-by-side match highlighting, group inspector, and named-capture export to JS/Python/PCRE.
Open regexlab
Related
jwt · url · mac address · semver version · ↗ generate RFC 5737/3849 test IPs