Regex guide
IP Address Regex Examples
IP address validation is a common regex use case for forms, logs, configuration files and network tools. This guide provides practical IPv4 and IPv6 regex examples with live tests.
Suggested pattern
^\d{1,3}(\.\d{1,3}){3}$
Common limitations
- Does not limit each group to 0–255.
- 999.999.999.999 still matches.
Suggested pattern
^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$
What it matches
192.168.1.1
255.255.255.255
Common limitations
- IPv4 only.
- Does not support CIDR suffixes.
Suggested pattern
^(10\.\d{1,3}\.\d{1,3}\.\d{1,3}|172\.(1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3})$
What it matches
10.0.0.1
172.16.5.10
192.168.1.1
Common limitations
- Does not strictly validate each octet as 0–255.
- Focused on private ranges only.
Suggested pattern
^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\/([0-9]|[12]\d|3[0-2])$
What it matches
192.168.1.0/24
10.0.0.0/8
Common limitations
- IPv4 CIDR only.
- Does not check whether the IP is a network address.
Suggested pattern
^([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}$
What it matches
2001:0db8:85a3:0000:0000:8a2e:0370:7334
Common limitations
- Does not support compressed IPv6 syntax such as ::1.
- Full IPv6 validation is much more complex.
Why validate IP addresses with regex?
IP address regex patterns are commonly used to validate user input, configuration files, network tools and API settings.
They help detect obvious formatting errors before an address is stored or processed by an application.
IPv4 vs IPv6
IPv4 addresses use four decimal numbers separated by dots, while IPv6 addresses use hexadecimal groups separated by colons.
Both formats are widely used today, and modern applications often need to support both.
What makes IPv6 validation difficult?
IPv6 supports several shorthand notations, including compressed zeros and abbreviated groups.
As a result, IPv6 regex patterns are significantly more complex than IPv4 patterns.
CIDR notation explained
CIDR notation combines an IP address with a network prefix length, such as 192.168.1.0/24.
It is commonly used in routing, firewalls, cloud infrastructure and network configuration.
Private vs public IP addresses
Private IP ranges are reserved for internal networks and are not directly reachable from the public internet.
Common private IPv4 ranges include 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16.
Can regex verify that an IP address exists?
A regex can validate the format of an IP address, but it cannot determine whether the address is reachable or currently assigned.
Network validation requires DNS lookups, routing checks or direct connectivity tests.
When to use IP address regex validation
IP address regex patterns are useful in firewall rules, server administration tools, cloud platforms, VPN configuration and logging systems.
They provide a fast first layer of validation before applying network-specific checks.