Regex and cron

Regex for IPv6 Addresses

Validate IPv6 addresses, including every legal way of compressing zeros with ::.

  • Free foreverNo trial, no paywall, no usage limits.
  • No sign-upOpen the page and start. We never ask for an email to use a tool.
  • PrivateEverything runs in your browser. Your input is never uploaded or stored on our servers.
  • Works offlineOnce the page has loaded, the tool keeps working without a connection.

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}|:))$/

Regular expression

JavaScript syntax, run by your browser's own engine in a background worker. Press / anywhere to jump here.
Reset to the original pattern and examplesShow cheat sheet: click any token to insert itCopy pattern
Find every match instead of stopping at the first. Needed for matchAll() and for replacing all occurrences.Case-insensitive: /abc/i also matches "ABC" and "aBc".^ and $ match at the start and end of every line, not just the whole string. Use it to test one value per line.Lets . match line breaks too. Without it, . stops at \n and \r.Treats the pattern as Unicode code points: enables \p{…} property escapes and \u{…}, makes emoji count as one character, and rejects sloppy escapes.Only matches starting exactly at lastIndex (position 0 here), never further along. Useful for tokenizers that consume input piece by piece.Records the start and end index of every capture group (match.indices). The tester always shows group positions, so this only changes your code.The upgraded unicode mode (ES2024): set operations like [\p{L}--[a-z]] and [A&&B], string properties like \p{RGI_Emoji}. Cannot be combined with u.
7 matches7 matches

Test string

Matches are highlighted as you type, in alternating colors. Click inside a match (or hover its row below) to color its capture groups.
7 matchesClear test string

Explanation

Every part of the pattern in plain English. Hover a row to highlight that part of the pattern.
Copy explanation

Loading the explainer…

Every match with its position and capture groups.Preview a replacement with $1, $<name> and $& references, then copy the result.Copy a ready-to-run snippet for JavaScript, Python, Go, C#, PHP or Dart.

Matches

Named groups are shown by name. undefined means the group did not take part in that match.
Copy matches as JSONDownload matches.csv
#MatchPosition
12001:0db8:85a3:0000:0000:8a2e:0370:73340–39
22001:db8:85a3::8a2e:370:733440–68
32001:db8::169–80
4::181–84
5::85–87
6fe80::88–94
7FE80::1FF:FE23:4567:890A95–119

Need a secure web app built around this? Talk to our team

Quick answer

An IPv6 regex has to list every place the :: compression can appear, so it is long: one alternative for eight full hextets and seven more for :: at the start, middle or end. The complete pattern matches 2001:db8::1, ::1, fe80:: and the full 2001:0db8:85a3:0000:0000:8a2e:0370:7334, and rejects two :: in one address, groups over four hex digits and nine-group addresses.

  • Tested examples
  • Token-by-token breakdown
  • Code in 5 languages
  • Live tester

How to use the IPv6 address regex

  1. 1Copy the pattern with Copy pattern in the tester, or take a ready-made snippet for your language from the code section below.
  2. 2Check the sample lines in Test string: every line the IPv6 address regex accepts is highlighted, rejected lines stay plain.
  3. 3Paste your own values one per line to see which pass, and read Explanation for what each part of the pattern does.
  4. 4Open the Code tab for JavaScript, Python, Go, C#, PHP or Dart with the pattern translated and escaped.
  5. 5Validate again on the server: client-side checks give users quick feedback, not security.

How does the IPv6 address regex work?

Read it left to right. Each piece below matches one part of the input:

  • (?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4} Eight full hextets: 1:2:3:4:5:6:7:8.
  • (?:[0-9A-Fa-f]{1,4}:){1,7}: One to seven hextets then :: at the end, like fe80::.
  • (?:[0-9A-Fa-f]{1,4}:){1,6}:[0-9A-Fa-f]{1,4} :: before the last hextet, like 2001:db8::1.
  • (?:…){1,5}(?::…){1,2} … (?:…){1,2}(?::…){1,5} The :: in every middle position, with at most seven hextets in total.
  • [0-9A-Fa-f]{1,4}:(?::[0-9A-Fa-f]{1,4}){1,6} One hextet, then :: and up to six more.
  • :(?:(?::[0-9A-Fa-f]{1,4}){1,7}|:) :: at the very start, like ::1, or the unspecified address :: itself.

Which strings does the IPv6 address regex match?

The whole input must match, from the first character to the last. Every row below is checked against the pattern by our automated test suite before each release.

InputMatches?Why
2001:0db8:85a3:0000:0000:8a2e:0370:7334YesFull form, eight hextets.
2001:db8:85a3::8a2e:370:7334YesZeros compressed in the middle, leading zeros dropped.
2001:db8::1YesThe documentation prefix with a host part.
::1YesLoopback.
::YesThe unspecified address.
fe80::YesLink-local prefix, compressed at the end.
FE80::1FF:FE23:4567:890AYesUppercase hex is allowed.
2001:db8::1::1No:: may appear only once.
12345::1NoA hextet has at most four hex digits.
2001:db8:85a3:0:0:8a2e:370:7334:1NoNine hextets.
gggg::1Nog is not a hex digit.
192.168.1.1NoAn IPv4 address.
:1NoA single leading colon is not valid.

Which IPv6 forms does this regex leave out?

Two legal forms are deliberately not covered, to keep the pattern readable:

  • Embedded IPv4, such as ::ffff:192.0.2.1 (IPv4-mapped) and 64:ff9b::192.0.2.1 (NAT64). These end in a dotted quad.
  • Zone IDs, such as fe80::1%eth0, which name the network interface for link-local addresses.

If your input can contain them, use a parser instead of growing the regex further: net.isIPv6() in Node.js, ipaddress.IPv6Address in Python, netip.ParseAddr in Go, IPAddress.TryParse in .NET or filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) in PHP. They also handle the address types (RFC 4291) for you.

How should I store and compare IPv6 addresses?

The same address has many spellings: 2001:DB8:0:0:0:0:0:1, 2001:db8::1 and 2001:0db8::0001 are identical. Comparing strings will report them as different. Normalize to the canonical form from RFC 5952 before storing or comparing: lowercase, no leading zeros, and :: replacing the longest run of zero groups. Python's str(ipaddress.ip_address(s)) and Go's netip.Addr.String() both produce it.

How do I use the IPv6 address regex in JavaScript, Python, Go, C# and PHP?

The pattern is translated for each engine rather than pasted as is: named groups, anchors, escapes, shorthand classes and flags are adjusted so each snippet behaves as closely to the JavaScript version as its engine allows. Differences that remain (Go's RE2 has no lookarounds or backreferences, for example) are called out under each one. The Code tab in the tester above also exports Dart, and updates as you edit the pattern.

JavaScript and TypeScript

const regex = /^(?:(?:[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}|:))$/;

function isValid(value) {
  return regex.test(value);
}

console.log(isValid("2001:0db8:85a3:0000:0000:8a2e:0370:7334"));

Python

import re

pattern = re.compile(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}|:))\Z")

def is_valid(value: str) -> bool:
    return pattern.search(value) is not None

print(is_valid("2001:0db8:85a3:0000:0000:8a2e:0370:7334"))
  • JavaScript's $ only matches at the very end of the string. Python's $ also matches before a trailing newline, so the pattern uses \Z instead.

Go

package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := 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}|:))$`)
	fmt.Println(re.MatchString("2001:0db8:85a3:0000:0000:8a2e:0370:7334"))
}

C#

using System;
using System.Text.RegularExpressions;

// The timeout stops a pathological input from hanging the thread (ReDoS).
var regex = new Regex(@"^(?:(?:[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}|:))\z", RegexOptions.None, TimeSpan.FromSeconds(1));
Console.WriteLine(regex.IsMatch("2001:0db8:85a3:0000:0000:8a2e:0370:7334"));
  • JavaScript's $ only matches at the very end of the string. .NET's $ also matches before a trailing newline, so the pattern uses \z instead.

PHP

<?php

$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}|:))$/D';

function isValid(string $value, string $pattern): bool
{
    return preg_match($pattern, $value) === 1;
}

var_dump(isValid('2001:0db8:85a3:0000:0000:8a2e:0370:7334', $pattern));
  • The D modifier makes $ match only at the very end, like JavaScript (PCRE's $ also matches before a trailing newline).

Browse all 25 patterns in the regex pattern library, or build your own in the regex tester.

Frequently asked questions

The :: shorthand can replace any run of zero groups, at the start, middle or end of the address. A regex cannot count groups on both sides of it, so the pattern lists each possible split as a separate alternative.
Yes: join the two patterns with | inside a non-capturing group, (?:ipv4|ipv6), with the anchors outside. In code it is usually clearer to try an IP parser and check which version it returns.
No. Hex digits can be uppercase or lowercase, but RFC 5952 recommends lowercase when you write or store an address.

Last updated by the Playxoft engineering team.

From the engineers who built this tool

Building something bigger than a quick fix?

Auth, payments, dashboards and security are where web apps get hard. We have shipped them many times. Share your idea and we will map out a realistic plan and estimate, free and with no obligation.

More free tools

See all tools

Regex Tester

Test regex live with highlighted matches, plain-English explanations and code export.

Open tool

Cron Expression Generator

New

Build and explain cron schedules, see the next runs in any time zone, copy platform snippets.

Open tool

SQL Formatter

Beautify or minify SQL in 12 dialects, with keyword case, indent and line:column errors.

Open tool

cURL to Python Converter

New

Convert cURL commands into Python requests or httpx code with params, json, files and auth.

Open tool

cURL to Fetch Converter

New

Turn any cURL command into JavaScript fetch() code with headers, JSON, forms and auth.

Open tool

cURL to Axios Converter

New

Convert cURL commands into an Axios request config with params, data, auth and timeouts.

Open tool