Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: start of string | Start of '192.168.1.1' |
(?: |
Non-capturing group start | Grouping for alternation |
25[0-5] |
Matches 250-255 | 255 |
| |
Alternation (OR) | Separates 25[0-5] from 2[0-4][0-9] |
2[0-4][0-9] |
Matches 200-249 | 249 |
| |
Alternation (OR) | Separates 2[0-4][0-9] from [01]?[0-9][0-9]? |
[01]? |
Optional 0 or 1 | 0 or 1 (e.g., '0' or '1') |
[0-9] |
Digit 0-9 | Any digit |
[0-9]? |
Optional digit | Optional second digit (e.g., '5' or '55') |
) |
Non-capturing group end | End of octet group |
\. |
Escaped dot (literal '.') | . |
(?: |
Non-capturing group start (repeated) | Grouping for repeated octets |
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) |
Same octet pattern as before | 255 |
\. |
Escaped dot (literal '.') | . |
){2} |
Repeat group exactly 2 times | Matches two octet-dot pairs |
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) |
Final octet (no trailing dot) | 1 |
) |
Non-capturing group end | End of full pattern |
$ |
Anchor: end of string | End of '192.168.1.1' |
Matches
0.0.0.0255.255.255.255192.168.1.110.0.0.1172.16.254.1
Does not match
256.1.1.1192.168.1192.168.1.1.1192.168.01.1192.168.1.1a1.2.3.04
Common pitfalls
- Leading zeros in octets (e.g., '010.0.0.1') are technically invalid in strict IPv4 but may pass in some implementations.
- Missing anchors (^/$) could allow partial matches (e.g., '192.168.1.1abc').
- Over-reliance on this pattern for validation without additional checks (e.g., leading zeros).
- Assuming this matches all valid IPv4 addresses (e.g., it does not handle edge cases like '0.0.0.0' with leading zeros).
Performance notes
Low risk of catastrophic backtracking due to fixed-length alternations and no nested quantifiers. The pattern is efficient for typical use cases.