Skip to content

Regex for Private Ipv4 — Pattern, Examples & Caveats

Pattern
^(10\.\d{1,3}\.\d{1,3}\.\d{1,3}|172\.(1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3})$

This pattern matches IPv4 addresses in the three RFC 1918 private ranges: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. It is used to detect internal addresses, for example in SSRF protection. Security-oriented patterns check sensitive or policy-bound input: password composition, card number shapes, tokens, private IP ranges and national identifiers. They are a first line of defense, never the whole story — pair them with checksums, real verification, encryption and access control. A regex can confirm a format but cannot prove a secret is valid or safe. It needs no special flags. Apply it to the whole input as a format contract. The \d{1,3} octets are not range-checked (0-255), so combine with a valid-IPv4 check or normalize first. It does not cover loopback (127.0.0.0/8), link-local (169.254.0.0/16) or other special ranges relevant to SSRF — block those too. For real SSRF defense, resolve DNS and check the actual IP after resolution, since hostnames can point to private addresses. 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

  • 10\.\d{1,3}\.\d{1,3}\.\d{1,3} — the 10.0.0.0/8 block
  • 172\.(1[6-9]|2\d|3[01]) — the 172.16-31 second octet range (172.16.0.0/12)
  • 192\.168\.\d{1,3}\.\d{1,3} — the 192.168.0.0/16 block
  • | — alternation between the three private ranges
  • ^...$ — anchors spanning the address

What it matches

10.0.0.1172.16.5.4192.168.1.1172.31.255.255

What it does not match

8.8.8.8172.32.0.111.0.0.1192.169.1.1

When to use it

Reach for a security pattern as one layer of input handling for sensitive or policy-bound data: password rules, card shapes, tokens, private ranges and national identifiers. Never treat a match as proof of validity or safety — combine it with checksums, signature verification, breach screening, encryption and strict access control. Also guard against ReDoS by avoiding nested quantifiers and bounding input length, since security checks are a tempting target for denial-of-service abuse.

Caveats

  • The \d{1,3} octets are not range-checked (0-255), so combine with a valid-IPv4 check or normalize first.
  • It does not cover loopback (127.0.0.0/8), link-local (169.254.0.0/16) or other special ranges relevant to SSRF — block those too.
  • For real SSRF defense, resolve DNS and check the actual IP after resolution, since hostnames can point to private addresses.

Code examples

JavaScript
const re = /^(10\.\d{1,3}\.\d{1,3}\.\d{1,3}|172\.(1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3})$/;
const input = "10.0.0.1";
if (re.test(input)) {
  // valid: proceed
} else {
  // reject with a helpful message
}
Python
import re

rx = re.compile(r"^(10\.\d{1,3}\.\d{1,3}\.\d{1,3}|172\.(1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3})$")
value = "10.0.0.1"
if rx.fullmatch(value):
    pass  # valid
else:
    raise ValueError("invalid format")
Java
import java.util.regex.*;

Pattern p = Pattern.compile("^(10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|172\\.(1[6-9]|2\\d|3[01])\\.\\d{1,3}\\.\\d{1,3}|192\\.168\\.\\d{1,3}\\.\\d{1,3})$");
Matcher m = p.matcher("10.0.0.1");
boolean ok = m.matches();   // whole input must conform
PHP
<?php
$subject = "10.0.0.1";
if (preg_match('/^(10\.\d{1,3}\.\d{1,3}\.\d{1,3}|172\.(1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3})$/', $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

What are the RFC 1918 private ranges?

10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 are reserved for private networks and are not routable on the public internet, which is why SSRF filters block them.

Is matching private IPs enough for SSRF protection?

No — also block loopback, link-local, multicast and reserved ranges, resolve DNS to the real IP, and re-check after redirects, because attackers use many encodings and rebinding tricks.

Is a regex enough to secure sensitive input?

Never on its own. Regex checks format; security also needs checksums (Luhn), real verification (token signatures, breach screening), encryption, masking and access control. Treat the pattern as one layer, not the solution.

How do I prevent ReDoS in security checks?

Avoid nested unbounded quantifiers, set a match timeout where the engine supports it, cap input length, and prefer simple linear patterns. Test suspicious inputs with long non-matching strings before deploying.

Advertisement
Ad slot · ref-in-article

Try the Regex Tester

Live regex matching with groups, flags and explanations.

Open tool →