Qaynaq LABS · TOOLS

REGEX · PYTHON

Regex explained

^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$

Matches a string representing an IPv4 address in dotted-decimal notation (e.g., 192.168.1.1). Each octet is 1 to 3 digits, followed by a dot, repeated 3 times, then a final octet without a trailing dot.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the start of the string Start of '192.168.1.1'
(?: Non-capturing group start (Python-specific) Groups '[0-9]{1,3}\\.', but does not capture
[0-9] Character class: matches any digit (0-9) Digit '1' in '192'
{1,3} Quantifier: matches 1 to 3 occurrences of the preceding element Digits '168' in '168'
\. Escaped literal dot (matches a single '.') Dot '.' in '192.'
) Non-capturing group end End of group for '[0-9]{1,3}\\.'
{3} Quantifier: matches exactly 3 occurrences of the preceding non-capturing group Three groups like '192.', '168.', '1.'
[0-9] Character class: matches any digit (0-9) Digit '1' in '1'
{1,3} Quantifier: matches 1 to 3 occurrences of the preceding element Digits '1' in '1'
$ Anchor: asserts the end of the string End of '192.168.1.1'

Matches

  • 0.0.0.0
  • 255.255.255.255
  • 192.168.1.1
  • 1.2.3.4
  • 123.45.67.89

Does not match

  • 256.1.1.1
  • 192.168.1
  • 192.168.1.1.1
  • 192.168.1.
  • .192.168.1.1
  • 192.168.01.1
  • 192.168.1.a
  • 192.168.1.1

Common pitfalls

  • Does not validate octet ranges (e.g., 256 is invalid but matches the pattern).
  • Does not account for leading zeros (e.g., '01' is technically invalid in strict IPv4 notation).
  • Fails if the string contains trailing whitespace or newlines (use re.MULTILINE or strip input).
  • Overly permissive for octet values (e.g., '999' is matched but invalid).
  • Does not handle IPv4-mapped IPv6 addresses (e.g., '::ffff:192.168.1.1').

Performance notes

No catastrophic backtracking risk due to fixed quantifiers and no alternations. Anchors (^ and $) ensure the entire string is checked, avoiding partial matches. For large inputs, pre-filter strings containing non-digit characters to improve performance.

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 use a non-capturing group (?:...) instead of a capturing group (...)?

Non-capturing groups are more efficient as they do not store the matched substring in memory or result groups, which is unnecessary for this pattern.

How to validate octet ranges (0-255) with this regex?

This regex alone cannot validate octet ranges. Use a more complex pattern like ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ for strict validation.

Does this regex work for IPv6 addresses?

No, this regex is specifically for IPv4 addresses. IPv6 addresses require a completely different pattern due to their hexadecimal and colon-separated format.

Why escape the dot (\\.) instead of using [.]?

Both \\. and [.] are valid for matching a literal dot, but \\. is more conventional and slightly more readable in this context. [.] is also correct and matches the same character.