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]|[01]?[0-9][0-9]?)$

Matches a valid IPv4 address in dotted-decimal notation, ensuring each octet is between 0 and 255, with no leading zeros unless the octet is 0.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of string
( Start of a capturing group (group 1) Start of group
( Start of an alternation group (group 2) Start of alternation
25[0-5] Matches numbers 250-255 (25 followed by 0-5) 250, 255
| Alternation: OR operator Separates alternatives
2[0-4][0-9] Matches numbers 200-249 (2 followed by 0-4, then 0-9) 200, 249
| Alternation: OR operator Separates alternatives
[01]?[0-9][0-9]? Matches numbers 0-199 with optional leading zero (0-1 followed by 0-9, optionally followed by another 0-9) 0, 5, 10, 99, 199
) End of alternation group (group 2) End of alternation
\. Escaped dot: matches a literal dot character .
) End of capturing group (group 1) End of group
{3} Quantifier: exactly 3 repetitions of the preceding group (group 1) Three octets followed by dots
( Start of an alternation group (group 3) Start of alternation
25[0-5] Matches numbers 250-255 (25 followed by 0-5) 250, 255
| Alternation: OR operator Separates alternatives
2[0-4][0-9] Matches numbers 200-249 (2 followed by 0-4, then 0-9) 200, 249
| Alternation: OR operator Separates alternatives
[01]?[0-9][0-9]? Matches numbers 0-199 with optional leading zero (0-1 followed by 0-9, optionally followed by another 0-9) 0, 5, 10, 99, 199
) End of alternation group (group 3) End of alternation
$ Anchor: asserts position at end of the string End of string

Matches

  • 0.0.0.0
  • 192.168.1.1
  • 255.255.255.255
  • 10.0.0.1
  • 172.16.254.1
  • 1.2.3.4

Does not match

  • 256.1.2.3
  • 192.168.01.1
  • 192.168.1
  • 192.168.1.1.1
  • abc.def.ghi.jkl
  • 192.168.1.-1
  • 192.168.001.001

Common pitfalls

  • Leading zeros in octets (e.g., 192.168.001.001) are technically allowed by the regex but may be invalid in some contexts.
  • The regex does not prevent leading zeros in octets like 01 or 001, which may be undesirable in strict IP validation.
  • The regex does not account for IP addresses with extra leading dots or spaces.
  • The regex does not validate the entire string if it contains trailing characters after the IP address.

Performance notes

The pattern is efficient for typical IPv4 validation as it uses simple alternations and quantifiers. However, catastrophic backtracking is not a concern here due to the fixed structure and lack of nested quantifiers. Anchoring with ^ and $ ensures the entire string is validated.

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 regex allow leading zeros in octets like 01 or 001?

The pattern [01]?[0-9][0-9]? allows optional leading zeros to match numbers like 0, 1, 01, 001, etc. This is intentional to cover all valid numeric representations, but may not align with strict IP address formatting rules in some contexts.

Can this regex validate IP addresses with extra leading or trailing characters?

No. The anchors ^ and $ ensure the entire string must match the IPv4 pattern exactly. Any extra characters (e.g., spaces, letters) will cause the match to fail.

Is this regex suitable for validating IP addresses in URLs or mixed strings?

No. This regex is designed to validate standalone IPv4 addresses. For use in URLs or mixed strings, additional logic (e.g., word boundaries) would be required to isolate the IP address.