Validation
Match an IPv4 address
Validate or extract IPv4 addresses like 192.168.1.1.
Pattern
Regular expression
(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)How it works
Strict IPv4 pattern that rejects out-of-range octets. Each of the four octets is 0–255 and does not allow leading zeros beyond a single one (e.g. `01` is invalid).
Matches
- ▸0.0.0.0
- ▸127.0.0.1
- ▸192.168.1.1
- ▸255.255.255.255
Non-matches
- ▸256.0.0.1
- ▸192.168.1
- ▸01.02.03.04
- ▸1.2.3.4.5
Common gotchas
The simpler `\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}` accepts invalid addresses like 999.999.999.999. Always use the range-checking form above for real validation.
Language snippets
JavaScript
const re = /(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)/g;
text.match(re);Python
re.findall(r"(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)", text)Go
re := regexp.MustCompile(`(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)`)
re.FindAllString(text, -1)Java
Pattern.compile("(?:(?:25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)").matcher(text)Related patterns
Match an IPv6 address
(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}|::1|::
Match a MAC address
(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}
Match an email address
[\w.+-]+@[\w-]+\.[\w.-]+
Match a URL
https?:\/\/[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=]*
Match a UUID (v4)
[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}
Match a US phone number
(?:\+?1[-. ]?)?\(?[2-9][0-9]{2}\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}