Regex for IPv4 Address — Pattern Explained with Examples
DodaTech
Updated Jun 20, 2026
2 min read
IPv4 address validation is fundamental for network configuration tools, security scanners, and any application that handles IP-based communication. Each IPv4 address consists of four octets ranging from 0 to 255, separated by dots. A simple pattern checks the format, while a complete pattern enforces the 0-255 range.
The Pattern
/^(\d{1,3}\.){3}\d{1,3}$/For strict 0-255 octet validation:
/^(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$/Pattern Breakdown
| Part | Meaning |
|---|---|
^ | Start-of-string anchor |
(\d{1,3}\.){3} | Three groups of 1-3 digits followed by a dot |
\d{1,3} | Final octet — 1 to 3 digits |
$ | End-of-string anchor |
25[0-5] | Numbers 250-255 |
2[0-4]\d | Numbers 200-249 |
1\d{2} | Numbers 100-199 |
[1-9]?\d | Numbers 0-99 |
Matches
- 192.168.1.1
- 10.0.0.1
- 255.255.255.255
- 0.0.0.0
- 8.8.8.8
Does NOT Match
- 256.1.2.3 (octet out of range)
- 192.168.1 (only 3 octets)
- 192.168.1.1.1 (5 octets)
- 192.168.1.abc (non-numeric)
- .0.0.1 (leading dot)
- 999.999.999.999 (all octets out of range)
Language Examples
JavaScript
const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/;
const strictIpv4Regex = /^(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$/;
console.log(strictIpv4Regex.test('192.168.1.1')); // true
console.log(strictIpv4Regex.test('256.1.2.3')); // false
Python
import re
pattern = r'^(\d{1,3}\.){3}\d{1,3}$'
print(bool(re.match(pattern, '192.168.1.1'))) # True
print(bool(re.match(pattern, '256.1.2.3'))) # True (simple pattern allows it)
strict = r'^(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$'
print(bool(re.match(strict, '256.1.2.3'))) # FalseRust
use regex::Regex;
fn main() {
let re = Regex::new(r"^(\d{1,3}\.){3}\d{1,3}$").unwrap();
println!("{}", re.is_match("192.168.1.1")); // true
}Common Pitfalls
- The simple
\d{1,3}pattern accepts numbers 000-999, so 999.999.999.999 passes format checks. Always use the full 0-255 pattern for strict validation - Leading zeros are valid (e.g.
192.168.001.001is a legitimate IPv4 address), but some applications reject them - Partial matches within larger text (e.g. matching
192.168.1.1insidehttp://192.168.1.1:8080/path) require lookarounds or separate tokenization - CIDR notation (e.g.
192.168.1.0/24) is a separate concept — the mask after the slash needs its own validation
Real-World Use Cases
- Network configuration forms — validate static IP entries in router or server admin panels
- Log analysis tools — extract and validate IP addresses from access logs for geo-location or threat detection
- Access control lists — verify IP address entries in firewall rules or allowlist configurations
FAQ
Related Patterns
Regex for IPv6 Address Regex for MAC Address
Previous
Regex for URL Validation — Pattern Explained with Examples
Next
Regex for IPv6 Address — Pattern Explained with Examples
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro