Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
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.0127.0.0.1192.168.1.1255.255.255.25510.0.0.1200.100.50.25
Does not match
256.0.0.1192.168.1192.168.1.1.1192.168.01.1192.168.1.01abc.def.ghi.jkl192.168.1.-1192.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.