Regex for IPv6 addresses

A regular expression for IPv6 addresses written in hexadecimal groups, including every way of compressing zeros with ::, checked against Python's own address parser, and a note on the forms it does not cover.

//

Matches

This tool runs in your browser. We never send what you type or paste to a server.

The pattern

/^(?:(?:[0-9a-f]{1,4}:){7}[0-9a-f]{1,4}|(?:[0-9a-f]{1,4}:){1,7}:|(?:[0-9a-f]{1,4}:){1,6}:[0-9a-f]{1,4}|(?:[0-9a-f]{1,4}:){1,5}(?::[0-9a-f]{1,4}){1,2}|(?:[0-9a-f]{1,4}:){1,4}(?::[0-9a-f]{1,4}){1,3}|(?:[0-9a-f]{1,4}:){1,3}(?::[0-9a-f]{1,4}){1,4}|(?:[0-9a-f]{1,4}:){1,2}(?::[0-9a-f]{1,4}){1,5}|[0-9a-f]{1,4}:(?::[0-9a-f]{1,4}){1,6}|:(?:(?::[0-9a-f]{1,4}){1,7}|:))$/i

RFC 4291, section 2.2 defines the text forms of an IPv6 address: eight groups of up to four hexadecimal digits separated by colons, where one run of zero groups may be replaced by ::. This pattern accepts every form that uses only hexadecimal groups. Besides the examples below, our tests compare it with Python's ipaddress parser on thousands of generated strings, and the two agree on every one.

The tester at the top of this page is loaded with this pattern and the examples below, one per line. It adds the m flag so that ^ and $ apply to each line, and g to show every match. Edit either box to try your own input.

How it works, token by token

PartMeaning
^(?:Start of the string and a group around nine alternatives. [0-9a-f]{1,4} below is one group of one to four hex digits; the i flag allows capitals.
(?:[0-9a-f]{1,4}:){7}[0-9a-f]{1,4}All eight groups written out: 2001:db8:0:0:0:ff00:42:8329.
|or
(?:[0-9a-f]{1,4}:){1,7}:One to seven groups, then :: at the end: 2001:db8::.
|or
(?:[0-9a-f]{1,4}:){1,6}:[0-9a-f]{1,4}Up to six groups, ::, one group: 2001:db8::1.
|or
(?:[0-9a-f]{1,4}:){1,5}(?::[0-9a-f]{1,4}){1,2}Up to five groups before :: and up to two after.
|or
(?:[0-9a-f]{1,4}:){1,4}(?::[0-9a-f]{1,4}){1,3}Up to four before and three after.
|or
(?:[0-9a-f]{1,4}:){1,3}(?::[0-9a-f]{1,4}){1,4}Up to three before and four after.
|or
(?:[0-9a-f]{1,4}:){1,2}(?::[0-9a-f]{1,4}){1,5}Up to two before and five after.
|or
[0-9a-f]{1,4}:(?::[0-9a-f]{1,4}){1,6}One group, ::, up to six groups: fe80::1:2.
|or
:(?:(?::[0-9a-f]{1,4}){1,7}|:):: at the start followed by up to seven groups (::1), or :: on its own, the unspecified address.
)$End of the group and of the string.

The alternatives exist because :: stands for one or more groups of zeros and may appear only once. Each alternative fixes how many groups come before it; the limits on each side keep the total at eight or fewer.

What it matches and what it rejects

InputResultWhy
2001:db8::1matchesCompressed.
2001:0db8:0000:0000:0000:ff00:0042:8329matchesFull form with leading zeros.
2001:DB8::FF00:42:8329matchesCapitals are allowed.
::1matchesLoopback.
::matchesThe unspecified address.
fe80::1matchesLink-local.
1:2:3:4:5:6:7::matches:: standing for a single group: legal, though RFC 5952 says not to write it.
2001:db8::1::1rejected:: twice: it would be impossible to tell how many zeros each one means.
2001:db8:0:0:0:0:0:0:1rejectedNine groups.
12345::1rejectedA group of five digits.
2001:db8::g1rejectedg is not hexadecimal.
2001:db8:::1rejectedThree colons.
::ffff:192.0.2.1rejectedIPv4 written at the end: valid IPv6, but not covered by this pattern.
fe80::1%eth0rejectedA zone index: also valid in some contexts and not covered.

What it leaves out

RFC 4291 also allows the last 32 bits to be written as an IPv4 address, as in ::ffff:192.0.2.1, the form for IPv4 addresses mapped into IPv6. Link-local addresses can carry a zone index after a percent sign, such as fe80::1%eth0, to say which network interface is meant. Both are valid in the right place and both are rejected here, to keep the pattern readable. If your input can contain them, use a parser instead of extending the regex. The examples use 2001:db8::/32, which RFC 3849 reserves for documentation.

Compare addresses after normalising them

One address has many spellings: 2001:DB8:0:0:0:0:0:1, 2001:db8::1 and 2001:0db8::0001 are the same. RFC 5952 defines a canonical text form: lowercase, no leading zeros, and the longest run of zero groups compressed. Convert to it before comparing, storing or logging addresses, otherwise an allow-list check can miss a match:

ip = ipaddress.ip_address('2001:DB8:0:0:0:0:0:1')
print(ip.compressed)   # → 2001:db8::1
print(ip.exploded)     # → 2001:0db8:0000:0000:0000:0000:0000:0001
print(ipaddress.ip_address('::ffff:192.0.2.1').ipv4_mapped)  # → 192.0.2.1
console.log(isIPv6('2001:db8::1'), isIPv6('2001:db8::1::1'));  // → true false
console.log(new URL('http://[2001:DB8:0:0:0:0:0:1]/').hostname);  // → [2001:db8::1]

In a URL an IPv6 address goes in square brackets, as in http://[2001:db8::1]:8080/, so that the port's colon is not confused with the address. The browser's URL parser returns the canonical form with the brackets.

When to use the regex at all

The pattern is useful where you cannot run a parser: a database CHECK constraint, a schema in JSON or XML, a log search in a tool that only takes regular expressions, or a quick form check in the browser. In application code, Python's ipaddress.ip_address, Node.js's net.isIPv6 and .NET's IPAddress.TryParse give a clear yes or no, handle the IPv4-mapped and zone forms, and tell you more about the address, such as whether it is link-local or private.

In JavaScript, Python, PHP and C#

JavaScript

const re = /^(?:(?:[0-9a-f]{1,4}:){7}[0-9a-f]{1,4}|(?:[0-9a-f]{1,4}:){1,7}:|(?:[0-9a-f]{1,4}:){1,6}:[0-9a-f]{1,4}|(?:[0-9a-f]{1,4}:){1,5}(?::[0-9a-f]{1,4}){1,2}|(?:[0-9a-f]{1,4}:){1,4}(?::[0-9a-f]{1,4}){1,3}|(?:[0-9a-f]{1,4}:){1,3}(?::[0-9a-f]{1,4}){1,4}|(?:[0-9a-f]{1,4}:){1,2}(?::[0-9a-f]{1,4}){1,5}|[0-9a-f]{1,4}:(?::[0-9a-f]{1,4}){1,6}|:(?:(?::[0-9a-f]{1,4}){1,7}|:))$/i;
console.log(re.test('2001:db8::1')); // → true
console.log(re.test('2001:db8::1::1')); // → false

Python

import re

pattern = re.compile(r'(?:(?:[0-9a-f]{1,4}:){7}[0-9a-f]{1,4}|(?:[0-9a-f]{1,4}:){1,7}:|(?:[0-9a-f]{1,4}:){1,6}:[0-9a-f]{1,4}|(?:[0-9a-f]{1,4}:){1,5}(?::[0-9a-f]{1,4}){1,2}|(?:[0-9a-f]{1,4}:){1,4}(?::[0-9a-f]{1,4}){1,3}|(?:[0-9a-f]{1,4}:){1,3}(?::[0-9a-f]{1,4}){1,4}|(?:[0-9a-f]{1,4}:){1,2}(?::[0-9a-f]{1,4}){1,5}|[0-9a-f]{1,4}:(?::[0-9a-f]{1,4}){1,6}|:(?:(?::[0-9a-f]{1,4}){1,7}|:))', re.IGNORECASE)
print(bool(pattern.fullmatch('2001:db8::1')))  # → True
print(bool(pattern.fullmatch('2001:db8::1::1')))  # → False

PHP

<?php
$pattern = '/^(?:(?:[0-9a-f]{1,4}:){7}[0-9a-f]{1,4}|(?:[0-9a-f]{1,4}:){1,7}:|(?:[0-9a-f]{1,4}:){1,6}:[0-9a-f]{1,4}|(?:[0-9a-f]{1,4}:){1,5}(?::[0-9a-f]{1,4}){1,2}|(?:[0-9a-f]{1,4}:){1,4}(?::[0-9a-f]{1,4}){1,3}|(?:[0-9a-f]{1,4}:){1,3}(?::[0-9a-f]{1,4}){1,4}|(?:[0-9a-f]{1,4}:){1,2}(?::[0-9a-f]{1,4}){1,5}|[0-9a-f]{1,4}:(?::[0-9a-f]{1,4}){1,6}|:(?:(?::[0-9a-f]{1,4}){1,7}|:))$/iD';
echo preg_match($pattern, '2001:db8::1'), "\n"; // → 1
echo preg_match($pattern, '2001:db8::1::1'), "\n"; // → 0

C#

using System;
using System.Text.RegularExpressions;

var pattern = new Regex(@"^(?:(?:[0-9a-f]{1,4}:){7}[0-9a-f]{1,4}|(?:[0-9a-f]{1,4}:){1,7}:|(?:[0-9a-f]{1,4}:){1,6}:[0-9a-f]{1,4}|(?:[0-9a-f]{1,4}:){1,5}(?::[0-9a-f]{1,4}){1,2}|(?:[0-9a-f]{1,4}:){1,4}(?::[0-9a-f]{1,4}){1,3}|(?:[0-9a-f]{1,4}:){1,3}(?::[0-9a-f]{1,4}){1,4}|(?:[0-9a-f]{1,4}:){1,2}(?::[0-9a-f]{1,4}){1,5}|[0-9a-f]{1,4}:(?::[0-9a-f]{1,4}){1,6}|:(?:(?::[0-9a-f]{1,4}){1,7}|:))\z", RegexOptions.IgnoreCase);
Console.WriteLine(pattern.IsMatch("2001:db8::1")); // → True
Console.WriteLine(pattern.IsMatch("2001:db8::1::1")); // → False

Each version checks the whole string. JavaScript's $ only matches at the very end, but Python, PHP and .NET also let $ match just before a final line break, so "abc\n" could slip through. The Python version uses re.fullmatch (so the ^ and $ are dropped), PHP adds the D modifier, and C# ends with \z.

The JavaScript, Python and C# examples are run before each update and their output is compared with the comments. PHP is not installed on our build machine, so the PHP version is checked against the PCRE pattern modifier documentation, and its string literal is checked to hand PHP exactly the pattern tested here.

Questions

Is there a short regex for IPv6?

Not one that is correct. Patterns such as ^[0-9a-f:]+$ accept nonsense like ::::, and the compressed :: form is what forces the alternatives. If a short check is enough, test for a colon and then pass the string to a parser.

Is IPv6 case-sensitive?

No. Hex digits may be uppercase or lowercase, which is why the pattern uses the i flag. RFC 5952 recommends lowercase when writing addresses.

Does this pattern match IPv4-mapped addresses like ::ffff:192.0.2.1?

No. They are valid IPv6, but covering them doubles the length of the pattern. Use a parser such as Python's ipaddress module if they can appear in your input.