breakitdown

Regex for an IPv4 address

Answer

Use ^(?:(?: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)$. Each octet is spelled out as 250-255, 200-249, 100-199, or 0-99, so 256 and 999 fail. Leading zeros such as 01 are rejected.

flags: g all matches · i ignore case · m ^ $ per line · s . matches newlines · u Unicode

JavaScript ✓PCRE ✓

No catastrophic-backtracking patterns found. (A heuristic check: nested and adjacent repeats, overlapping alternatives.)

  1. ^Start of the stringanchor
  2. (?:…)Non-capturing group: groups without rememberinggroup
  3. (?:…)Non-capturing group: groups without rememberinggroup
  4. |Either of 4 alternatives, tried left to right; the first that leads to an overall match winsalternation
  5. Alternative 1:
  6. 25The text "25" literallyliteral
  7. [0-5]One character: '0' to '5'character class
  8. Alternative 2:
  9. 2The character '2' literallyliteral
  10. [0-4]One character: '0' to '4'character class
  11. \dA digit (0-9)character type
  12. Alternative 3:
  13. 1The character '1' literallyliteral
  14. \dA digit (0-9)character type
  15. \dA digit (0-9)character type
  16. Alternative 4:
  17. [1-9]One character: '1' to '9'character class
  18. ?Repeat the class optionally (zero or one time)quantifier
  19. \dA digit (0-9)character type
  20. \.The character '.' literallyliteral
  21. {3}Then repeat the whole group exactly 3 timesquantifier
  22. (?:…)Non-capturing group: groups without rememberinggroup
  23. |Either of 4 alternatives, tried left to right; the first that leads to an overall match winsalternation
  24. Alternative 1:
  25. 25The text "25" literallyliteral
  26. [0-5]One character: '0' to '5'character class
  27. Alternative 2:
  28. 2The character '2' literallyliteral
  29. [0-4]One character: '0' to '4'character class
  30. \dA digit (0-9)character type
  31. Alternative 3:
  32. 1The character '1' literallyliteral
  33. \dA digit (0-9)character type
  34. \dA digit (0-9)character type
  35. Alternative 4:
  36. [1-9]One character: '1' to '9'character class
  37. ?Repeat the class optionally (zero or one time)quantifier
  38. \dA digit (0-9)character type
  39. $End of the stringanchor

Examples

Checked at build time in JavaScript and in Python's re.

InputMatches?
192.168.1.1✓ match
255.255.255.255✓ match
0.0.0.0✓ match
256.1.1.1✗ no match
01.2.3.4✗ no match
1.2.3✗ no match
1.2.3.4.5✗ no match
192.168.1.1/24✗ no match
10.0.0.1✗ no match
999.999.999.999✗ no match

Try your own text

One string per line: each line is tested on its own (the m flag is added here).

How it works

A regex cannot compare numbers, so the range 0-255 has to be written as text ranges. 25[0-5] covers 250-255, 2[0-4]\d covers 200-249, 1\d\d covers 100-199, and [1-9]?\d covers 0-99 without a leading zero. The first group repeats three times with a dot; the last octet has no dot after it.

Rejecting leading zeros is deliberate. Some parsers, including the C library's inet_aton, read 010 as octal 8, so 010.0.0.1 can mean 8.0.0.1 to one program and 10.0.0.1 to another. That mismatch has caused real access-control bypasses. Python's ipaddress module has rejected leading zeros since 3.9.5 for the same reason.

Variants

Gotchas

Related explainers

← Explain any regular expression