IPv4 and IPv6 Address Regex Matching
Learn how to validate IP addresses using regular expressions, including both IPv4 and IPv6 formats with comprehensive examples.
IPv4 and IPv6 Address Regex Matching
IP address validation is essential for network configuration, security filtering, and geolocation services. Regular expressions provide a powerful way to validate and extract IP addresses from text. In this comprehensive guide, we'll explore patterns for both IPv4 and IPv6 validation.
Understanding IP Address Formats
IPv4 Format
IPv4 addresses consist of four octets (0-255) separated by dots:
- Example: 192.168.1.1
- Format: A.B.C.D where each segment is 0-255
- Range: 0.0.0.0 to 255.255.255.255
IPv6 Format
IPv6 addresses use hexadecimal notation with colons:
- Example: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
- Format: 8 groups of 4 hex digits separated by colons
- Range: 0000:0000:0000:0000:0000:0000:0000:0000 to ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
- Shorthand: Can use :: to represent multiple zero groups
IPv4 Validation Patterns
Basic IPv4 Pattern
^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$
Matches: 192.168.1.1, 10.0.0.1, 255.255.255.255
Issue: Allows invalid values like 999.999.999.999
Corrected IPv4 Pattern
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
This pattern validates each octet is 0-255:
Breakdown:
25[0-5]- Matches 250-2552[0-4][0-9]- Matches 200-249[01]?[0-9][0-9]?- Matches 0-199\.- Literal dot{3}- Three times (first three octets)- Final group matches last octet
Valid IPv4: 192.168.1.1, 10.0.0.1, 255.255.255.255, 0.0.0.0
Invalid IPv4: 999.999.999.999, 256.0.0.1, 192.168.1, 192.168.1.1.1
IPv4 Pattern Explained
The key is validating each octet is 0-255:
| Pattern | Range |
|---|---|
25[0-5] | 250-255 |
2[0-4][0-9] | 200-249 |
[01]?[0-9][0-9]? | 0-199 |
Combined, these match exactly 0-255.
IPv6 Validation Patterns
Basic IPv6 Pattern
^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$
Matches full IPv6 addresses like: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
IPv6 with Shorthand Support
IPv6 allows shorthand notation:
::represents one or more groups of zeros- Leading zeros in groups can be omitted
^(([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}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$
This comprehensive pattern handles:
- Full IPv6 addresses
- Shorthand (::) notation
- IPv6 with embedded IPv4
- Link-local addresses (fe80::)
Valid IPv6:
2001:0db8:85a3:0000:0000:8a2e:0370:7334(full)2001:db8:85a3::8a2e:370:7334(shorthand)::1(localhost)2001::192.168.1.1(IPv6 with IPv4)
Universal IP Address Pattern
Detect Both IPv4 and IPv6
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^(([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}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$
This pattern validates either IPv4 or IPv6 using alternation (|).
Extracting IP Addresses from Text
Find IPv4 in Text
\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
Extracts from: "Server at 192.168.1.1 is responding" → 192.168.1.1
Find IPv6 in Text
\b(([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}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\b
Extracts from: "Connect to 2001:db8::1 for IPv6" → 2001:db8::1
Find Any IP Address
\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b|([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}
This simpler pattern finds common formats without full validation.
Special IP Address Patterns
Private IPv4 Addresses
^(10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.|192\.168\.)\d{1,3}\.\d{1,3}$
Matches: 10.0.0.1, 172.16.0.1, 192.168.1.1
Private ranges:
- 10.0.0.0 - 10.255.255.255
- 172.16.0.0 - 172.31.255.255
- 192.168.0.0 - 192.168.255.255
Loopback Addresses
^(127\.0\.0\.1|::1)$
Matches: 127.0.0.1 (IPv4), ::1 (IPv6)
Public IPv4 Only (Not Private)
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|(?!10\.)(?!172\.(1[6-9]|2[0-9]|3[0-1])\.)(?!192\.168\.)(?!127\.0\.0\.1)
Best Practices
1. Use Appropriate Validation Level
// For simple filtering
const basicIPv4 = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
// For strict validation
const strictIPv4 = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
2. Combine with Validation Libraries
// Use regex for initial validation
if (ipv4Regex.test(ip)) {
// Use specialized library for detailed validation
if (!isValidIPAddress(ip)) {
return false;
}
}
3. Consider Performance
// IPv4 is much faster to validate
if (ip.includes('.')) {
return ipv4Pattern.test(ip);
} else if (ip.includes(':')) {
return ipv6Pattern.test(ip);
}
Common Pitfalls
Pitfall 1: Not Validating Octet Range
// BAD: Allows invalid values
^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$
// GOOD: Validates 0-255
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
Pitfall 2: Forgetting Word Boundaries
// BAD: Matches partial IPs
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
// GOOD: Only matches complete IPs
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Pitfall 3: Case Sensitivity for IPv6
// BAD: Won't match uppercase
/^[0-9a-f:]+$/
// GOOD: Matches both cases
/^[0-9a-fA-F:]+$/i
Testing Your IP Validation
Use our interactive Regex Tester with these test cases:
Valid IPv4:
- 192.168.1.1
- 10.0.0.1
- 255.255.255.255
- 0.0.0.0
Invalid IPv4:
- 999.999.999.999
- 256.0.0.1
- 192.168.1
- abc.def.ghi.jkl
Valid IPv6:
- 2001:0db8:85a3:0000:0000:8a2e:0370:7334
- 2001:db8:85a3::8a2e:370:7334
- ::1
- fe80::1
Invalid IPv6:
- 2001:0db8:85a3::8a2e::7334 (multiple ::)
- 2001:0db8:85a3:zzzz:0000:8a2e:0370:7334 (invalid hex)
Conclusion
IP address validation with regex requires attention to detail, especially for IPv6. For most applications, the IPv4 pattern ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ provides robust validation.
For IPv6, consider using a specialized validation library due to the complexity of shorthand notation and edge cases.
Remember to test your patterns with real IP addresses from your environment, including private addresses, loopback addresses, and IPv6 with various shorthand notations.
Experiment with different patterns using our Regex Generator to find the perfect fit for your specific IP validation needs!
About the Author
The Regex Master Team consists of experienced developers and technical writers dedicated to simplifying regular expressions for everyone. We ensure all patterns are rigorously tested and verified to provide accurate, production-ready solutions.
Try It: Regex Tester
Use our interactive regex tester to experiment with the patterns you learned in this article. Test your regular expressions in real-time and see immediate results.