DevKits

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

Try any pattern live in the Regex Tester