Regex for IPv6 address
IPv6 address with abbreviations supported.
The IPv6 address regex is ^(([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}|:))$ — copy it below, or open it in the explainer for a token-by-token breakdown.
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}|:))$
What it matches
2001:0db8:85a3:0000:0000:8a2e:0370:73342001:db8::1::1fe80::::
What it doesn't match
2001:db8::1::2::g1.2.3.4
Notes & gotchas
Handles full form, leading/trailing :: compression, and edge cases like ::1 (loopback) and :: (all zeros). The full RFC 4291 grammar is more complex; this covers the common forms.
Code in your language
Use the explainer's Code tab to generate ready-to-paste snippets in JavaScript, Python, Java, .NET, Go, Ruby, and PHP for this pattern.
Open in explainer →Token-by-token breakdown
Every part of the pattern, left to right:
| Token | Meaning |
|---|---|
^ | start of string (or line in multiline mode) |
( | start of a capturing group |
( | start of a capturing group |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
: | literal text “:” |
){7} | end of group, exactly 7 times |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
| | OR — try the alternative |
( | start of a capturing group |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
: | literal text “:” |
){1,7} | end of group, between 1 and 7 times |
: | literal text “:” |
| | OR — try the alternative |
( | start of a capturing group |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
: | literal text “:” |
){1,6} | end of group, between 1 and 6 times |
: | literal text “:” |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
| | OR — try the alternative |
( | start of a capturing group |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
: | literal text “:” |
){1,5} | end of group, between 1 and 5 times |
( | start of a capturing group |
: | literal text “:” |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
){1,2} | end of group, between 1 and 2 times |
| | OR — try the alternative |
( | start of a capturing group |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
: | literal text “:” |
){1,4} | end of group, between 1 and 4 times |
( | start of a capturing group |
: | literal text “:” |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
){1,3} | end of group, between 1 and 3 times |
| | OR — try the alternative |
( | start of a capturing group |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
: | literal text “:” |
){1,3} | end of group, between 1 and 3 times |
( | start of a capturing group |
: | literal text “:” |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
){1,4} | end of group, between 1 and 4 times |
| | OR — try the alternative |
( | start of a capturing group |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
: | literal text “:” |
){1,2} | end of group, between 1 and 2 times |
( | start of a capturing group |
: | literal text “:” |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
){1,5} | end of group, between 1 and 5 times |
| | OR — try the alternative |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
: | literal text “:” |
( | start of a capturing group |
: | literal text “:” |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
){1,6} | end of group, between 1 and 6 times |
| | OR — try the alternative |
: | literal text “:” |
( | start of a capturing group |
( | start of a capturing group |
: | literal text “:” |
[0-9a-fA-F]{1,4} | between 1 and 4 times: any of: digits, a–f, A–F |
){1,7} | end of group, between 1 and 7 times |
| | OR — try the alternative |
: | literal text “:” |
) | end of group |
) | end of group |
$ | end of string (or line in multiline mode) |
About this pattern
Identifying and validating user identity (names, contact info, IDs) is one of the most common reasons developers reach for regex. The pattern below handles the format check; for full validation always confirm against a source of truth (database, API, or document).
Quick usage in different languages
This exact pattern — with the correct escaping and idioms for each language:
- JavaScript:
/^(([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}|:))$/.test(value) - Python:
re.match(r"^(([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}|:))$", value) - Java:
Pattern.compile("^(([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}|:))$").matcher(value).matches() - C# / .NET:
Regex.IsMatch(value, @"^(([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}|:))$") - Go:
regexp.MustCompile(`^(([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}|:))$`).MatchString(value) - Ruby:
/^(([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}|:))$/.match?(value) - PHP:
preg_match('~^(([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}|:))$~', $value)
The explainer’s Code tab regenerates these for any pattern you paste, and the downloadable cheat sheet bundles the breakdown, all seven snippets, and the pitfalls below onto one printable page.
Common pitfalls
- Anchored to the whole string. This pattern uses ^ and $, so it requires the entire input to match. To find it inside a longer text, drop the anchors and use the global (g) flag.
- Case sensitivity. Letter ranges are case-sensitive — use the i flag if the input case can vary.
- Validate beyond format. Matching the format doesn't guarantee the value is real. Confirm the ipv6 address against a source of truth (database, API, or checksum) where it matters.
Standards & sources
This pattern is based on the following authoritative specification(s) and issuing authorities. Formats can change — always confirm against the primary source.
Related patterns
More patterns in the Identity & contact category:
See also
Browse all 300 patterns in the library, or open this regex in the interactive explainer for a token-by-token breakdown, live testing, and code in seven languages.
Want more patterns? Browse the full library →