Regex Pattern for IP Addresses
IP address validation is trickier than it looks because each octet must be between 0 and 255, which pure character-class regex cannot enforce. This example uses a proper numeric range pattern for IPv4. IPv6 validation is also shown with the abbreviated notation allowed by the spec. Use the tester to verify edge cases like leading zeros and broadcast addresses.
Example
/^((25[0-5]|2[0-4]d|[01]?dd?).){3}(25[0-5]|2[0-4]d|[01]?dd?)$/
# Test cases:
192.168.1.1
10.0.0.0
255.255.255.255
256.0.0.1
192.168.1
999.999.999.999
0.0.0.0FAQ
- Why does IP regex need numeric range groups?
- A simple \d{1,3} would match 999, which is not a valid octet. The range groups 25[0-5], 2[0-4]\d, and [01]?\d\d? together cover exactly 0–255.
- How do I validate IPv6 addresses?
- IPv6 has a more complex structure with optional abbreviations (::). For production use, consider a dedicated IP address validation library rather than regex.
- Does this pattern match CIDR notation like 192.168.0.0/24?
- No. To match CIDR notation, append an optional group like (\/([12]?\d|3[0-2]))? to the end of the IPv4 pattern.
Related Examples
Regex Pattern for URL Matching
Matching URLs reliably requires handling protocols, domains, ports, paths, query...
Regex Pattern for Hex ColorsHex color codes appear in CSS, design tokens, and configuration files. This exam...
Regex Pattern for Date FormatsDate strings come in many formats and regex can catch obvious formatting errors ...