IP Address (IPv4 & IPv6) Regular Expression Studio
Generate strict IPv4 and IPv6 validation regular expressions with our Network Address Regex Studio! Build precise octet boundary matchers (restricting values strictly to 0โ255 without allowing invalid numbers like 256), Classless Inter-Domain Routing (CIDR) subnet masks (`/24`, `/64`), and compressed IPv6 hextets.
Select Preset Rules
Pattern Tokens Explanation
Here is a step-by-step breakdown of how regular expression engines evaluate your formulated IP validation rules:
Reference Patterns
| Addressing Type | Match Example | Regex Snippet |
|---|---|---|
| IPv4 (Strict / RFC) | 192.168.1.1 | ^(?:(?: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]?)$ |
| IPv4 (Relaxed) | 999.999.999.999 | ^(?:\d{1,3}\.){3}\d{1,3}$ |
| IPv6 Complete Format | 2001:0db8:85a3:0000:0000:8a2e:0370:7334 | ^([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}$ |
| IPv6 (Compressed Shortened) | 2001:db8::1 | ^(([0-9a-fA-F]{1,4}:){1,7}:|::)$ |
| IPv4 Subnet / CIDR Mask | 10.0.0.0/24 | ^(?:(?: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]?)(?:\/(?:3[0-2]|[12]?[0-9]))$ |
| IPv4 with Socket Port | 127.0.0.1:8080 | ^(?:(?: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-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))$ |
| IPv6 Subnet / CIDR Mask | 2001:db8::/32 | ^([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}(?:\/(?:12[0-8]|1[0-1][0-9]|[1-9]?[0-9]))$ |
| Localhost Loopback IPv4 | 127.0.0.1 | ^127\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ |
| Private Network Class A | 10.50.8.254 | ^10\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ |
| Private Network Class B | 172.16.254.1 | ^172\.(?:1[6-9]|2[0-9]|3[0-1])\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ |
| Private Network Class C | 192.168.1.1 | ^192\.168\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ |
| Dual Stack Universal IP | 8.8.8.8 | ^(?:(?:(?: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,7}[0-9a-fA-F]{1,4})$ |
Entropy Analysis
| Character Pool Segment | Dimension Size | Entropy Bits/Char |
|---|---|---|
| Digits (0-9) | 10 | 3.32 bits |
| Hex Characters (a-f) | 6 | 2.58 bits |
| Separator Dot (.) | 1 | 1.00 bits |
| Separator Colon (:) | 1 | 1.00 bits |
| CIDR Slash separator (/) | 1 | 1.00 bits |
| Hex Case Boundaries (A-F) | 6 | 2.58 bits |
| Loopback Signifier (127) | 3 | 1.58 bits |
| CIDR Subnet Digits | 10 | 3.32 bits |
Entropy Analysis in regular expressions evaluates the information density and structural complexity of matched patterns based on Shannon's Entropy formula ($H = -\\sum P_i \\log_2 P_i$). Here is how it works:
- Information Density: Measures the unpredictability and strictness of character classes. A pattern with higher entropy restricts inputs more precisely, leaving fewer opportunities for structural anomalies.
- Character Pool Segmenting: Breaks down matched values into operational blocks (digits, spaces, hyphens, prefixes, parentheses) and calculates their corresponding bit pools.
- ReDoS Vulnerability Protection: Helps developers analyze pattern backtracking depth. Low-entropy, overly loose patterns (like overlapping wildcards) can trigger catastrophic backtracking, causing servers to hang under ReDoS exploits. High-entropy, precise patterns mitigate this risk.
Overview & Capabilities
Generate strict IPv4 and IPv6 validation regular expressions with our Network Address Regex Studio! Build precise octet boundary matchers (restricting values strictly to 0โ255 without allowing invalid numbers like 256), Classless Inter-Domain Routing (CIDR) subnet masks (/24, /64), and compressed IPv6 hextets.
How to Use
/0 to /32 or /128) or port numbers (:8080).Key Features
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) to prevent illegal numbers > 255.::1, 2001:db8::1).192.168.1.0/24).127.0.0.1:3000)..htaccess and Nginx server blocks.Common Use Cases
Tips & Best Practices
Frequently Asked Questions
Q Why is `\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}` flawed for IPv4 validation?
Because `\d{1,3}` accepts any 3-digit number (including invalid values like 300 or 999). A strict IPv4 regex must test numerical ranges 0โ255 explicitly across all four octets.
Q What is the complete pattern for strict IPv4 validation?
`^(?:(?: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]?)$`.
Q How are compressed IPv6 addresses validated in regular expressions?
IPv6 regexes use alternation branches matching 8 full 16-bit hextets (`[0-9a-fA-F]{1,4}`) or variations containing `::` indicating omitted consecutive zero fields.





