Skip to content

Regex for Ipv4 — Pattern, Examples & Caveats

Pattern
^((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)$

This pattern validates an IPv4 address by matching four octets separated by dots, where each octet is constrained to the numeric range 0-255 using alternation rather than a loose digit run. It rejects out-of-range values like 999. Validation patterns answer a yes/no question about an entire input: does this string conform to the expected format? They are anchored with ^ and $ so the whole value must match, and they belong at the edge of your system — form fields, API payloads and import files — where rejecting bad input early prevents downstream errors. A validation regex is a contract check, not a transformation. It needs no special flags. Apply it to the whole input as a format contract. It rejects leading zeros (01.2.3.4) by design; some systems interpret those as octal, so rejecting them avoids ambiguity. IPv6 needs a completely different pattern — this one is IPv4-only. For production, prefer a dedicated IP library (ipaddress in Python, net in Go) that also handles CIDR and normalization. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.

Advertisement
Ad slot · ref-top

Token-by-token explanation

  • ^ — start anchor
  • ( ... \.){3} — three octets each followed by a literal dot
  • 25[0-5] — matches 250-255
  • 2[0-4]\d — matches 200-249
  • 1\d\d — matches 100-199
  • [1-9]?\d — matches 0-99 without leading zeros
  • ( ... )$ — the fourth octet, then end anchor

What it matches

192.168.1.10.0.0.0255.255.255.2558.8.8.8

What it does not match

256.1.1.11.2.31.2.3.4.501.2.3.41.2.3.999

When to use it

Reach for a validation pattern when data crosses a trust boundary: a signup form, an API request body, a CSV import or a configuration value. Run it before any processing so malformed input is rejected with a clear message instead of causing a cryptic failure later. Keep the pattern in one shared place, document the exact format it accepts, and pair it with a real parser when the value has semantics (dates, numbers, addresses) that a shape check alone cannot confirm.

Caveats

  • It rejects leading zeros (01.2.3.4) by design; some systems interpret those as octal, so rejecting them avoids ambiguity.
  • IPv6 needs a completely different pattern — this one is IPv4-only.
  • For production, prefer a dedicated IP library (ipaddress in Python, net in Go) that also handles CIDR and normalization.

Code examples

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)$/;
const input = "192.168.1.1";
if (re.test(input)) {
  // valid: proceed
} else {
  // reject with a helpful message
}
Python
import re

rx = re.compile(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)$")
value = "192.168.1.1"
if rx.fullmatch(value):
    pass  # valid
else:
    raise ValueError("invalid format")
Java
import java.util.regex.*;

Pattern p = 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)$");
Matcher m = p.matcher("192.168.1.1");
boolean ok = m.matches();   // whole input must conform
PHP
<?php
$subject = "192.168.1.1";
if (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)$/', $subject, $m)) {
    // $m[0] is the full match
}

How to test it

Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.

Frequently asked questions

Why use alternation instead of \d{1,3}?

\d{1,3} would accept 999, which is not a valid octet. The alternation 25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d enforces the true 0-255 range.

Does it match 1.2.3.04?

No — the [1-9]?\d branch forbids leading zeros, so 04 is rejected. This is intentional to avoid octal-interpretation ambiguity.

Should validation regexes be anchored?

Yes — use ^ and $ (or fullmatch) so the entire input must conform. Without anchors, a valid-looking substring inside garbage would pass, letting malformed data through your validation layer.

What is catastrophic backtracking and how do I avoid it?

Nested quantifiers like (a+)+ can explode exponentially on non-matching input, freezing the engine (ReDoS). Avoid ambiguous nesting, prefer atomic groups or possessive quantifiers where supported, and bound input length.

Advertisement
Ad slot · ref-in-article

Try the Regex Tester

Live regex matching with groups, flags and explanations.

Open tool →