Qaynaq LABS · TOOLS

REGEX · PYTHON

Regex explained

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$

Matches a valid IPv4 address in dotted-decimal notation, ensuring each octet is between 0 and 255 inclusive.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of '192.168.1.1'
(?: Non-capturing group start (atomic grouping in Python) Start of group for first three octets
(?: Non-capturing group start (subgroup for octet pattern) Start of subgroup for first octet
25[0-5] Matches numbers 250-255 250, 255
| Alternation (OR operator) Separates '25[0-5]' from next alternative
2[0-4][0-9] Matches numbers 200-249 200, 249
| Alternation (OR operator) Separates '2[0-4][0-9]' from next alternative
[01]?[0-9][0-9]? Matches numbers 0-199 with optional leading zero 0, 1, 19, 199
) Non-capturing group end End of subgroup for first octet
\. Escaped literal dot (matches '.') The '.' in '192.168.1.'
){3} Quantifier: repeat preceding group exactly 3 times Three occurrences of octet+dot pattern
(?: Non-capturing group start (subgroup for last octet pattern) Start of subgroup for final octet
25[0-5] Matches numbers 250-255 250, 255
| Alternation (OR operator) Separates '25[0-5]' from next alternative
2[0-4][0-9] Matches numbers 200-249 200, 249
| Alternation (OR operator) Separates '2[0-4][0-9]' from next alternative
1[0-9][0-9] Matches numbers 100-199 100, 199
| Alternation (OR operator) Separates '1[0-9][0-9]' from next alternative
[1-9]?[0-9] Matches numbers 0-99 with optional leading digit (0-9) 0, 5, 99
) Non-capturing group end End of subgroup for last octet
$ Anchor: asserts position at end of the string End of '192.168.1.1'

Matches

  • 0.0.0.0
  • 127.0.0.1
  • 192.168.1.1
  • 255.255.255.255
  • 10.0.0.1
  • 200.100.50.25

Does not match

  • 256.0.0.1
  • 192.168.1
  • 192.168.1.1.1
  • 192.168.01.1
  • 192.168.1.01
  • abc.def.ghi.jkl
  • 192.168.1.-1
  • 192.168.1.256

Common pitfalls

  • Leading zeros are allowed but not required (e.g., '010' is treated as '10')
  • Does not validate IP address semantics (e.g., private vs public ranges)
  • Fails on addresses with extra leading zeros like '192.168.001.001'
  • Does not handle IPv4-mapped IPv6 addresses

Performance notes

The pattern is efficient for valid IPv4 addresses but may exhibit catastrophic backtracking on malformed strings due to nested quantifiers and alternations. Anchoring (^ and $) ensures full-string validation, preventing partial matches. Consider pre-filtering input length to avoid excessive backtracking.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Why does the pattern use non-capturing groups (?:...) instead of capturing groups?

Non-capturing groups are used for grouping without capturing, improving performance by reducing memory overhead. The regex only needs to validate the pattern, not extract substrings.

Can this pattern match IPv4 addresses with leading zeros like '010.000.000.001'?

Yes, the pattern [01]?[0-9][0-9]? and [1-9]?[0-9] allow leading zeros, so '010.000.000.001' would match. However, note that leading zeros are not standard in IPv4 notation.

Does this pattern validate the semantic correctness of an IPv4 address?

No, it only validates the syntactic structure and numeric range of each octet. It does not check for reserved, private, or multicast ranges.