IPv6 Address Validation Regex
• CronOS Team
regexipv6validationtutorialnetworking
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!
IPv6 Address Validation Regex
Validate IPv6 addresses in standard format with 8 groups of 1-4 hexadecimal digits separated by colons.
Pattern Breakdown
regex
^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$
Components
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
([0-9a-fA-F]{1,4}:){7} | First 7 groups | Exactly 7 groups of 1-4 hex digits + colon |
[0-9a-fA-F]{1,4} | Final group | Last group of 1-4 hex digits (no trailing colon) |
$ | End anchor | Ensures match to string end |
Character Classes
[0-9a-fA-F]- Hexadecimal digits: 0-9, a-f, A-F{1,4}- Quantifier: between 1 and 4 occurrences{7}- Quantifier: exactly 7 occurrences:- Literal colon separator
Examples
Valid:
2001:0db8:85a3:0000:0000:8a2e:0370:73342001:db8:85a3:0:0:8a2e:370:7334(compressed zeros)2001:db8:85a3::8a2e:370:7334(double colon compression - not supported by this pattern)::1(localhost - not supported by this pattern)fe80::1(compressed - not supported by this pattern)
Invalid:
2001:0db8:85a3:0000:0000:8a2e:0370(only 7 groups)2001:0db8:85a3:0000:0000:8a2e:0370:7334:1234(9 groups)2001:0db8:85a3:0000:0000:8a2e:0370:733g(invalid hex character)2001:0db8:85a3:0000:0000:8a2e:0370:73345(group too long, 5 digits)2001::db8:85a3:0000:0000:8a2e:0370:7334(double colon not supported)
Implementation
JavaScript
javascript
const ipv6Regex = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/;
ipv6Regex.test('2001:0db8:85a3:0000:0000:8a2e:0370:7334'); // true
ipv6Regex.test('2001:db8:85a3:0:0:8a2e:370:7334'); // true
ipv6Regex.test('2001:0db8:85a3:0000:0000:8a2e:0370'); // false (incomplete)
ipv6Regex.test('2001::db8:85a3:0000:0000:8a2e:0370:7334'); // false (double colon)
Python
python
import re
ipv6_regex = r'^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$'
bool(re.match(ipv6_regex, '2001:0db8:85a3:0000:0000:8a2e:0370:7334')) # True
bool(re.match(ipv6_regex, '2001:db8:85a3:0:0:8a2e:370:7334')) # True
bool(re.match(ipv6_regex, '2001::db8:85a3:0000:0000:8a2e:0370:7334')) # False (double colon)
Go
go
ipv6Regex := regexp.MustCompile(`^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$`)
ipv6Regex.MatchString("2001:0db8:85a3:0000:0000:8a2e:0370:7334") // true
ipv6Regex.MatchString("2001:db8:85a3:0:0:8a2e:370:7334") // true
ipv6Regex.MatchString("2001::db8:85a3:0000:0000:8a2e:0370:7334") // false (double colon)
Limitations
- No zero compression: Doesn't support
::(double colon) for compressed zeros - No IPv4-mapped IPv6: Doesn't support
::ffff:192.168.1.1format - Strict format: Requires all 8 groups explicitly (no compression)
- No leading zero compression: Doesn't handle
2001:db8::1format - Format only: Validates format, not network reachability
When to Use
- Strict IPv6 format validation
- When you need uncompressed IPv6 addresses
- Network configuration that requires explicit format
- When zero compression is not allowed
- Basic IPv6 format checking
For production, consider:
- Supporting zero compression (
::) for more flexible validation - Supporting IPv4-mapped IPv6 addresses
- Using IPv6 parsing libraries for comprehensive validation
- Handling mixed IPv4/IPv6 formats
- Validating against reserved IPv6 ranges if needed
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!