tools / regex library / ipv6 address
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

InputResult
2001:0db8:85a3:0000:0000:8a2e:0370:7334matches
fe80::1matches
::1matches
2001:db8::ff00:42:8329matches
192.168.1.1 (IPv4)rejects
g001::1 (g not hex)rejects
:::1rejects

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

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