Visual regex workbench

Build, import, explain, test and share regex visually.

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.

Simple IPv4

Matches four groups of 1 to 3 digits separated by dots.

Suggested pattern

^\d{1,3}(\.\d{1,3}){3}$
Quick test

What it matches

  • 192.168.1.1
  • 8.8.8.8

Common limitations

  • Does not limit each group to 0–255.
  • 999.999.999.999 still matches.

Strict IPv4

Validates IPv4 octets from 0 to 255.

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)$
Quick test

What it matches

  • 192.168.1.1
  • 255.255.255.255

Common limitations

  • IPv4 only.
  • Does not support CIDR suffixes.

Private IPv4 ranges

Matches common private IPv4 ranges: 10.x.x.x, 172.16–31.x.x and 192.168.x.x.

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})$
Quick test

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.

IPv4 with CIDR

Matches an IPv4 address followed by a CIDR prefix from /0 to /32.

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])$
Quick test

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.

Basic IPv6

Matches full IPv6 addresses made of 8 hexadecimal groups.

Suggested pattern

^([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}$
Quick test

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.