Validation
Match an IPv6 address
Match IPv6 addresses in canonical or compressed form.
Pattern
Regular expression
(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}|::1|::How it works
The pattern above matches full 8-group IPv6 plus common loopback shorthand. Full RFC 4291 conformance (all `::` compression positions) requires a much larger pattern — for real work, use an inet_pton call.
Matches
- ▸2001:db8::1
- ▸fe80::1
- ▸2001:0db8:0000:0000:0000:ff00:0042:8329
- ▸::1
Non-matches
- ▸1.2.3.4
- ▸gggg::
- ▸12345::
Common gotchas
Matching every valid compressed IPv6 form with regex alone is a well-known nightmare. Use language-native parsing (`net.ParseIP` in Go, `ipaddress` in Python) when correctness matters.
Language snippets
JavaScript
const re = /(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}|::1|::/g;Python
import ipaddress
try: ipaddress.IPv6Address("2001:db8::1")
except ValueError: passGo
import "net"
addr := net.ParseIP("2001:db8::1")
if addr != nil && addr.To4() == nil { /* v6 */ }Java
Pattern.compile("(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}|::1|::").matcher(text)Frequently asked questions
What is the regex for IPv6 address?
The pattern (?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}|::1|:: matches IPv6 address. The pattern above matches full 8-group IPv6 plus common loopback shorthand. Full RFC 4291 conformance (all `::` compression positions) requires a much larger pattern — for real work, use an inet_pton call.
What does this IPv6 address pattern match?
It matches strings like 2001:db8::1, fe80::1, 2001:0db8:0000:0000:0000:ff00:0042:8329, but rejects 1.2.3.4, gggg::.
How do I use this pattern in JavaScript, Python, Go, Java?
Ready-to-run snippets are provided for JavaScript, Python, Go, Java. Copy the one for your language from the Language snippets section — each wraps the same core pattern in that language's regex API.
What are common pitfalls with a IPv6 address regex?
Matching every valid compressed IPv6 form with regex alone is a well-known nightmare. Use language-native parsing (`net.ParseIP` in Go, `ipaddress` in Python) when correctness matters.
Related patterns
Match an IPv4 address
(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)
Match a MAC address
(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}
Match an email address
[\w.+-]+@[\w-]+\.[\w.-]+
Match a URL
https?:\/\/[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=]*
Match a UUID (v4)
[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}
Match a US phone number
(?:\+?1[-. ]?)?\(?[2-9][0-9]{2}\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}