Regular expression
/^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/Pattern breakdown
| Part | Meaning |
|---|---|
Anchor or context | Use anchors when you need whole-string validation. |
Main token | The core token sequence describes the accepted text shape. |
Character class | Character classes limit which characters are valid. |
Quantifier | Quantifiers control how many characters or groups are accepted. |
Flags | Use language-specific flags such as i, m, or u only when needed. |
Should match
192.168.1.1
Should not match
256.1.1.1not matching sample192.168.1.1 invalid256.1.1.1192.168.1.1
Test cases
| Input | Expected | Why it matters |
|---|---|---|
192.168.1.1 | Match | Representative valid input for this pattern. |
256.1.1.1 | No match | Common invalid or boundary input. |
(empty string) | No match | Common invalid or boundary input. |
not matching sample | No match | Common invalid or boundary input. |
192.168.1.1 invalid | No match | Common invalid or boundary input. |
256.1.1.1192.168.1.1 | No match | Common invalid or boundary input. |
JavaScript
const re = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/;
re.test(input);Python
import re
bool(re.search(r"^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$", text))PHP
$ok = preg_match('/^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/', $value) === 1;Java
Pattern pattern = Pattern.compile("^(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)$");
pattern.matcher(value).find();Go
re := regexp.MustCompile(`^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$`)
ok := re.MatchString(value)Notes and production use
IPv4 Address regex is useful as a practical starting point. Test it against your real input, avoid using it as the only security control, and prefer a parser when the format has complex grammar.
Performance tip: avoid running complex regular expressions repeatedly on very large untrusted strings without limits. Prefer anchored validation patterns, cap input length before matching, and use a parser when the target format has nested grammar.