Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts the position at the start of the string | Start of 'abc123/def' |
[a-z0-9] |
Character class: matches any lowercase letter or digit | 'a', '1' |
+ |
Quantifier: matches one or more of the preceding token | 'abc123' |
(?: |
Non-capturing group start: groups tokens without capturing | Start of '(?:\.[a-z0-9]+)*' |
\. |
Escaped character: matches a literal dot | '.' in 'abc.123' |
[a-z0-9] |
Character class: matches any lowercase letter or digit | 'a', '1' |
)+ |
Quantifier: matches zero or more occurrences of the preceding group | '.123' in 'abc.123' |
\/ |
Escaped character: matches a literal forward slash | '/' in 'abc/def' |
[a-z0-9-] |
Character class: matches any lowercase letter, digit, or hyphen | 'a', '1', '-' |
+ |
Quantifier: matches one or more of the preceding token | 'def-456' |
$ |
Anchor: asserts the position at the end of the string | End of 'abc123/def' |
Matches
abc/defa1b2c3/456test123/foo-barx/y123/456
Does not match
abc.def/ghiabc/def/ghiabc def/ghiABC/defabc//def/abc/defabc/def-abc/def.
Common pitfalls
- Forgetting to escape the forward slash (\/) can lead to incorrect matches.
- The pattern does not allow uppercase letters, which may be unexpected in some contexts.
- The pattern does not allow underscores or other special characters in the segments.
- The pattern does not allow empty segments (e.g., 'a..b/c' or 'a//b').
Performance notes
The pattern is efficient as it uses non-capturing groups and avoids nested quantifiers. No catastrophic backtracking risk. Ensure input strings are not excessively long to maintain performance.