IPv6 Address Regular Expression – Free, Online, Private
Matches a fully written IPv6 address of eight hextets. Compressed forms need a parser.
- 2001:0db8:85a3:0000:0000:8a2e:0370:7334
The pattern
A fully expanded IPv6 address is eight hextets separated by colons. This pattern matches that shape and nothing shorter. It does not match ::1 and it does not match 2001:db8::1. Those addresses are legal and common, and a single regular expression that accepts every legal compression is long enough to hide a mistake. The honest page is the one that matches the fully written form and points you at a parser for the rest.
The source is (?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}. The flags on this page are i. i means case-insensitive. m means ^ and $ match at line breaks, which is how the sample can show several candidates. g is not set. The tester finds non-overlapping matches, and without g a pattern with no anchor still reports the matches it walks. The box under the title is a tester with this pattern and the sample already filled in. Edit either one. Nothing you type is uploaded. The same tester, without a preset, is the regex tool.
How to read it
[0-9A-Fa-f]{1,4} is one to four hex digits. The group (?:hextet:){7} requires seven of those followed by a colon, and one more hextet closes the address. That is eight groups. There is no alternative for ::. Adding :: as its own alternative would also match the double colon inside a compressed address and report a success the rest of the address did not earn. The i flag is redundant with the character class and harmless. There is no boundary, so the finder can sit inside a longer log token. Anchors belong on a form field.
What it will not do
Zone indexes such as fe80::1%eth0 are not matched. IPv4-mapped tails such as ::ffff:192.0.2.1 are not matched. Leading zeros are allowed in a hextet because the class does not care. Any double colon is the gap, including the loopback ::1. If you need compression, use the platform parser: URL, ipaddress, or net.ParseIP. Use this expression to highlight the fully written form. The sample leaves ::1 and 2001:db8::1 unmatched on purpose.
A match is not a parsed value. An email pattern does not send mail, a UUID pattern does not allocate an id, and a password pattern does not store a password. When the next step needs a real parser, use one: the URL constructor, a date library, a semver library, or the converters on this site. The expression is the part you can see.
JavaScript, Python, and Go
The JavaScript snippet uses the RegExp constructor so the source does not have to be escaped for a literal slash. The Python snippet uses a normal string with the same escapes JSON would emit, which keeps \d as a regex digit class. Go's snippet uses a raw string. If the pattern contains a lookahead, the Go snippet says so and does not call MustCompile, because RE2 would reject it at runtime.
JavaScriptconst re = new RegExp("(?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}", "i");
const found = text.match(re);
console.log(found ? found[0] : "no match");
Pythonimport re
pattern = re.compile("(?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}", re.IGNORECASE)
found = pattern.search(text)
print(found.group(0) if found else "no match")
Gopackage main
import (
"fmt"
"regexp"
)
func main() {
text := ""
re := regexp.MustCompile(`(?i)(?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}`)
fmt.Println(re.FindString(text))
}
Frequently asked questions
Why doesn't 2001:db8::1 match?
A single :: that replaces a run of zeros is legal IPv6 and is not what this expression covers. Use a parser for the general case.
Does ::1 match?
No. The loopback compression is two colons, not eight hextets. The pattern refuses it so a shorter token cannot look successful.
Are uppercase hex digits allowed?
Yes. The class includes A-F and a-f, and the i flag is on.
Is a full eight-hextet address matched?
Yes. Seven hextet-and-colon groups plus a final hextet.