Back to Home

IPv4 Address Validation Regex

CronOS Team
regexipv4validationtutorialnetworking

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern

IPv4 Address Validation Regex

Validate IPv4 addresses with proper octet range validation (0-255) using a comprehensive regex pattern.

Pattern Breakdown

regex
^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)$

Components

ComponentDescriptionMatches
^Start anchorEnsures match from string start
(?:...)Non-capturing groupGroups octet pattern
(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)Octet patternValidates 0-255 range
\.SeparatorLiteral dot
{3}QuantifierExactly 3 occurrences (first 3 octets)
(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)Final octetLast octet (0-255)
$End anchorEnsures match to string end

Detailed Octet Breakdown

Each octet pattern (?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d) matches:

  • 25[0-5] - 250-255
  • 2[0-4]\d - 200-249
  • 1\d\d - 100-199
  • [1-9]\d - 10-99
  • \d - 0-9

Examples

Valid:

  • 192.168.1.1
  • 10.0.0.1
  • 255.255.255.255
  • 0.0.0.0
  • 172.16.0.1
  • 8.8.8.8

Invalid:

  • 256.1.1.1 (octet exceeds 255)
  • 192.168.1 (only 3 octets)
  • 192.168.1.1.1 (5 octets)
  • 192.168.01.1 (leading zero, though format valid)
  • 192.168.1 (incomplete)
  • 192.168.1. (trailing dot)

Implementation

JavaScript

javascript
const ipv4Regex = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)$/;
ipv4Regex.test('192.168.1.1'); // true
ipv4Regex.test('255.255.255.255'); // true
ipv4Regex.test('0.0.0.0'); // true
ipv4Regex.test('256.1.1.1'); // false (exceeds 255)
ipv4Regex.test('192.168.1'); // false (incomplete)

Python

python
import re
ipv4_regex = r'^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)$'
bool(re.match(ipv4_regex, '192.168.1.1'))  # True
bool(re.match(ipv4_regex, '255.255.255.255'))  # True
bool(re.match(ipv4_regex, '256.1.1.1'))  # False (exceeds 255)

Go

go
ipv4Regex := regexp.MustCompile(`^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)$`)
ipv4Regex.MatchString("192.168.1.1") // true
ipv4Regex.MatchString("255.255.255.255") // true
ipv4Regex.MatchString("256.1.1.1") // false (exceeds 255)

Limitations

  1. Leading zeros: Accepts 192.168.01.1 (technically valid format, but non-standard)
  2. No reserved IP validation: Doesn't check for reserved/private IP ranges
  3. Format only: Validates format, not network reachability
  4. No CIDR support: Doesn't validate subnet masks (e.g., /24)
  5. Complex pattern: Can be harder to read and maintain

When to Use

  • IPv4 address format validation
  • Network configuration forms
  • IP input validation
  • Firewall rule validation
  • When you need strict octet range checking

For production, consider:

  • Rejecting leading zeros for stricter validation
  • Validating reserved/private IP ranges if needed
  • Using IP parsing libraries for additional validation
  • Supporting CIDR notation if required
  • Combining with network reachability checks

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern