IPv4 AddressRegex — Pattern, Examples & Code
^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$What it matches / doesn't match
| Matches ✅ | Does not match ❌ |
|---|---|
●192.168.1.1 | ●256.1.1.1 |
●10.0.0.0 | ●192.168.1 |
●255.255.255.0 | ●1.2.3.4.5 |
How it works
Each of the four octets is matched by a branch that accepts 250–255, 200–249, or 0–199 (with optional leading zero for single and double digit values). The pattern ensures each octet stays within the valid 0–255 range without relying on post-match numeric comparison. Leading zeros like 192.168.01.1 are permitted by this pattern.
Code Usage
JavaScript
const regex = /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
regex.test("192.168.1.1"); // truePython
import re
pattern = re.compile(r'^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
bool(pattern.match("192.168.1.1")) # TrueGo
import "regexp"
r := regexp.MustCompile(`^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$`)
r.MatchString("192.168.1.1") // trueNeed a custom regex for a different use case?
Generate a custom regex with AI →FAQ
What does the IPv4 Address regex pattern match?
Each of the four octets is matched by a branch that accepts 250–255, 200–249, or 0–199 (with optional leading zero for single and double digit values). The pattern ensures each octet stays within the valid 0–255 range without relying on post-match numeric comparison. Leading zeros like 192.168.01.1 are permitted by this pattern.
How do I use the IPv4 Address regex in JavaScript?
const regex = /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/; regex.test("192.168.1.1"); // true
Can I use the IPv4 Address regex in Python?
Yes. import re pattern = re.compile(r'^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$') bool(pattern.match("192.168.1.1")) # True
