Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | '' in '+123456789012345' |
\+? |
Optional literal '+' character (quantifier '?' means 0 or 1 occurrence) | '+' in '+123456789012345' or '' in '123456789012345' |
[1-9] |
Character class: matches a single digit from 1 to 9 (no leading zero) | '1' in '123456789012345' |
\d |
Character class: matches any digit (0-9) | '2' in '123456789012345' |
{1,14} |
Quantifier: matches between 1 and 14 occurrences of the preceding token (\d) | '23456789012345' in '123456789012345' |
$ |
Anchor: asserts position at end of the string | '' in '+123456789012345' |
Matches
123456789012345+1234567890123459+9
Does not match
01234567890123451234567890123456+0123456789012345123456789012345a++123456789012345+
Common pitfalls
- Forgetting to escape the '+' character, which is a special regex metacharacter (must be '\+').
- Assuming the pattern allows leading zeros (it does not due to [1-9]).
- Not anchoring with ^ and $, which could lead to partial matches in longer strings.
- Overlooking that the total length (including optional '+') is 1 to 16 characters.
Performance notes
No catastrophic backtracking risk. The pattern is linear in complexity (O(n)) and efficient for typical use cases. Anchors (^ and $) ensure no unnecessary backtracking occurs.