Skip to content
Regex for IPv6 Address — Pattern Explained with Examples

Regex for IPv6 Address — Pattern Explained with Examples

DodaTech Updated Jun 20, 2026 2 min read

IPv6 address validation is increasingly important as the internet transitions from IPv4. An IPv6 address has 8 groups of 1-4 hexadecimal digits separated by colons. The :: shorthand collapses consecutive zero groups, and the last two groups may represent an embedded IPv4 address.

The Pattern

Simple full form:

/^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/

Pattern Breakdown

PartMeaning
^Start-of-string anchor
([0-9a-fA-F]{1,4}:){7}Seven groups of 1-4 hex digits each followed by a colon
[0-9a-fA-F]{1,4}Final eighth group of 1-4 hex digits
$End-of-string anchor

For full validation including :: shorthand and embedded IPv4, a much longer pattern is required.

Matches

  • 2001:0db8:85a3:0000:0000:8a2e:0370:7334
  • ::1 (loopback)
  • fe80::1 (link-local)
  • 2001:db8::1 (compressed)
  • ::ffff:192.168.1.1 (IPv4-mapped)

Does NOT Match

  • 2001:db8 (only one group)
  • xyzr:1::1 (invalid hex characters)
  • 2001::85a3::1 (double :: compression)
  • 192.168.1.1 (plain IPv4)
  • 2001:db8:85a3:0:0:8a2e:370:7334:extra (too many groups)

Language Examples

JavaScript

const ipv6Regex = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/;
console.log(ipv6Regex.test('2001:0db8:85a3:0000:0000:8a2e:0370:7334')); // true
console.log(ipv6Regex.test('192.168.1.1'));                               // false

Python

import re
pattern = r'^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$'
print(bool(re.match(pattern, '2001:0db8:85a3:0000:0000:8a2e:0370:7334')))  # True
print(bool(re.match(pattern, '192.168.1.1')))                               # False

Ruby

pattern = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/
puts pattern.match?('2001:0db8:85a3:0000:0000:8a2e:0370:7334')  # true
puts pattern.match?('192.168.1.1')                               # false

Common Pitfalls

  • The :: shorthand can only appear once in an address — 2001::85a3::1 is invalid because the expansion is ambiguous
  • Leading zeros in each group are optional — 2001:db8::1 is the same as 2001:0db8:0000:0000:0000:0000:0000:0001
  • IPv4-compatible IPv6 addresses (like ::ffff:192.168.1.1) mix formats and need special handling in the pattern
  • Zone identifiers (e.g. fe80::1%eth0) are valid for link-local addresses but most regexes do not include the %interface suffix

Real-World Use Cases

  • Network configuration tools — validate IPv6 addresses entered in static network settings or DHCPv6 configurations
  • Firewall rule engines — verify IPv6 addresses in security policy entries and allowlists
  • DNS management systems — validate AAAA record entries before committing changes to DNS zones

FAQ

How do I handle the :: shorthand in an IPv6 regex?
The simplest approach is to validate the expanded form first, then preprocess the input by expanding :: to the correct number of zero groups before validation. A single regex that handles both forms is extremely long.
Should I use regex or an IP library for IPv6 validation?
Use an IP address library (like Python’s ipaddress module or Node’s net module) for production code. They handle shorthand, embedded IPv4, zone IDs, and edge cases correctly.

Related Patterns

Regex for IPv4 Address Regex for MAC Address

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro