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!
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
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
(?:...) | Non-capturing group | Groups octet pattern |
(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d) | Octet pattern | Validates 0-255 range |
\. | Separator | Literal dot |
{3} | Quantifier | Exactly 3 occurrences (first 3 octets) |
(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d) | Final octet | Last octet (0-255) |
$ | End anchor | Ensures 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-2552[0-4]\d- 200-2491\d\d- 100-199[1-9]\d- 10-99\d- 0-9
Examples
Valid:
192.168.1.110.0.0.1255.255.255.2550.0.0.0172.16.0.18.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
- Leading zeros: Accepts
192.168.01.1(technically valid format, but non-standard) - No reserved IP validation: Doesn't check for reserved/private IP ranges
- Format only: Validates format, not network reachability
- No CIDR support: Doesn't validate subnet masks (e.g.,
/24) - 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!