Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | Start of string |
(?: |
Non-capturing group start | Start of group |
(?: |
Non-capturing group start (inner) | Start of inner group |
25[0-5] |
Matches numbers 250-255 | 250, 255 |
| |
Alternation (OR operator) | Separates alternatives |
2[0-4][0-9] |
Matches numbers 200-249 | 200, 249 |
| |
Alternation (OR operator) | Separates alternatives |
[01]?[0-9][0-9]? |
Matches numbers 0-199 (with optional leading 0) | 0, 99, 199 |
) |
Non-capturing group end (inner) | End of inner group |
\. |
Escaped literal dot (matches '.') | . |
){3} |
Quantifier: repeat the preceding group exactly 3 times | Repeats group 3 times |
(?: |
Non-capturing group start (outer) | Start of outer group |
25[0-5] |
Matches numbers 250-255 | 250, 255 |
| |
Alternation (OR operator) | Separates alternatives |
2[0-4][0-9] |
Matches numbers 200-249 | 200, 249 |
| |
Alternation (OR operator) | Separates alternatives |
[01]?[0-9][0-9]? |
Matches numbers 0-199 (with optional leading 0) | 0, 99, 199 |
) |
Non-capturing group end (outer) | End of outer group |
: |
Literal colon | : |
\d+ |
Matches one or more digits (0-9) | 80, 443, 65535 |
) |
Non-capturing group end | End of group |
$ |
Anchor: asserts position at end of the string | End of string |
Matches
192.168.1.1:8010.0.0.1:443255.255.255.255:655350.0.0.0:0172.16.254.1:8080
Does not match
256.1.2.3:80192.168.1.1:65536192.168.1:80192.168.1.1.1:80192.168.1.1:abc192.168.1.1:80192.168.1.1:
Common pitfalls
- Forgetting to escape the dot (.) in the IPv4 pattern, which would match any character instead of a literal dot.
- Not anchoring the pattern with ^ and $, which could lead to partial matches within longer strings.
- Assuming the port number can be more than 5 digits (e.g., 65536), which is invalid for standard ports.
- Overlooking the optional leading zero in octets (e.g., 010 is treated as 10, not 8 in octal).
- Not accounting for the non-capturing groups, which could lead to unnecessary capturing overhead.
Performance notes
The pattern is well-structured and avoids catastrophic backtracking due to the use of non-capturing groups and quantifiers. However, the alternation with multiple nested groups may slightly impact performance for very long strings. Anchoring with ^ and $ ensures the entire string is validated, preventing partial matches.