Regex Library
Regex Library

10 patterns

Network & IP Regex Patterns

Network-related validation patterns for IP addresses, MAC addresses, CIDR blocks, and other networking primitives. Essential for infrastructure tools, firewall rule validation, and network configuration parsers.

Common Use Cases

IP address validationNetwork config parsingFirewall rulesInfrastructure as code

All Network & IP Patterns

IPv4 Address

Validates IPv4 addresses (0.0.0.0 - 255.255.255.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]?)$

IPv4 with CIDR Notation

Validates IPv4 addresses with CIDR notation (e.g., 192.168.1.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]?)\/(?:[0-9]|[1-2][0-9]|3[0-2])$

MAC Address

Standard 48-bit MAC address (colon or hyphen separated).

^(?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2})$

Port Number

Validates port numbers (1-65535).

^([1-9][0-9]{0,3}|[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])$

MAC Address (Colon Format)

Validates MAC address in colon-separated format (AA:BB:CC:DD:EE:FF)

^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$

IPv6 Address

Validates full and compressed IPv6 addresses.

^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::(?:[0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}$|^[0-9a-fA-F]{1,4}::(?:[0-9a-fA-F]{1,4}:){0,5}[0-9a-fA-F]{1,4}$

Hostname (Single Label)

Validates a single DNS hostname label (no dots, 1-63 characters).

^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$

FQDN (Fully Qualified Domain Name)

Validates fully qualified domain names (hostname.domain.tld).

^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$

IPv4 Subnet Mask

Validates IPv4 subnet masks (only valid octet values).

^(255|254|252|248|240|224|192|128|0)\.((255|254|252|248|240|224|192|128|0)\.){2}(255|254|252|248|240|224|192|128|0)$

IPv4 with Port

Validates an IPv4 address with a port number (1–65535) in host:port form.

^(?:(?: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])$

Frequently Asked Questions

What's the simplest IPv4 regex?

^(\d{1,3}\.){3}\d{1,3}$ - but this allows 999.999.999.999. For strict validation, use the full pattern that checks 0-255 ranges.

How do I validate an IPv6 address?

IPv6 has many valid formats including compressed notation (::1). Use the IPv6 Address pattern in the library.

What is a MAC address regex?

^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$ - accepts both colon and hyphen separators.

Looking for patterns in other categories?

Browse all 209 patterns