Regex for IPv6 Address
This regex matches standard IPv6 addresses in their full notation with eight groups of four hexadecimal digits separated by colons. IPv6 is the successor to IPv4, providing a vastly larger address space. This pattern matches the expanded form of IPv6 addresses, which is the most explicit representation used in configuration files and documentation.
^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$ What is the regex pattern for IPv6 Address?
The regex pattern for IPv6 Address is ^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$ with the i flag. This regex matches standard IPv6 addresses in their full notation with eight groups of four hexadecimal digits separated by colons. IPv6 is the successor to IPv4, providing a vastly larger address space. This pattern matches the expanded form of IPv6 addresses, which is the most explicit representation used in configuration files and documentation. This pattern is commonly used for network configuration validation and dns record validation.
Test Examples
2001:0db8:85a3:0000:0000:8a2e:0370:7334 2001:0db8:85a3:0000:0000:8a2e:0370:7334 fe80:0000:0000:0000:0000:0000:0000:0001 fe80:0000:0000:0000:0000:0000:0000:0001 not:an:ipv6 Common Uses
- ✓ Network configuration validation
- ✓ DNS record validation
- ✓ Firewall rules
- ✓ Server log parsing
Variations
With shorthand (::)
^(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?(::)(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?$ Allows :: shorthand for consecutive zero groups
IPv6 with IPv4 suffix
^([0-9a-fA-F]{1,4}:){6}(\d{1,3}\.){3}\d{1,3}$ Matches mixed notation like ::ffff:192.168.1.1
Loopback address
^::1$ Matches only the IPv6 loopback address
Frequently Asked Questions
Why does this not match shortened IPv6 addresses?
The base pattern matches only full-form addresses with all eight groups. IPv6 allows shortening with :: to represent consecutive zero groups, which makes the regex significantly more complex. Use the 'With shorthand' variation for that.
How many addresses does IPv6 support?
IPv6 uses 128-bit addresses, providing approximately 340 undecillion (3.4 x 10^38) unique addresses. This is vastly more than IPv4's approximately 4.3 billion addresses.
When should I validate IPv6 addresses with regex?
Use regex for quick format checks in forms or log parsers. For production network code, use your language's built-in IP address parsing library, which handles all valid representations including compressed and mixed notation.