Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts the position at the start of the string | Start of input |
( |
Start of a non-capturing group (optional country code block) | |
\+? |
Optional plus sign (quantifier ? makes it optional) | + or nothing |
\d{1,3} |
1 to 3 digits (country code) | 1, 44, 999 |
[- ]? |
Optional separator: either a hyphen or space (quantifier ? makes it optional) | - or space or nothing |
)? |
End of the non-capturing group and makes the entire group optional | |
\(? |
Optional opening parenthesis (quantifier ? makes it optional) | ( |
\d{3} |
Exactly 3 digits (area code) | 123 |
\)? |
Optional closing parenthesis (quantifier ? makes it optional) | ) |
[- ]? |
Optional separator: either a hyphen or space (quantifier ? makes it optional) | - or space or nothing |
\d{3} |
Exactly 3 digits (exchange code) | 456 |
[- ]? |
Optional separator: either a hyphen or space (quantifier ? makes it optional) | - or space or nothing |
\d{4} |
Exactly 4 digits (subscriber number) | 7890 |
$ |
Anchor: asserts the position at the end of the string | End of input |
Matches
+1 (123) 456-789044 123 456 7890123-456-7890(123)45678901234567890+999 123 456 78901-800-555-1234
Does not match
+12345678901212312345123-456123-456-789123.456.7890abc1234567890123)456-7890
Common pitfalls
- Overly permissive with separators leading to ambiguous matches
- Does not validate country codes or area codes for correctness
- May match invalid sequences like '123)456-7890' due to optional parentheses
- No strict validation of digit groupings (e.g., area code '000' is allowed but invalid in real phone numbers
Performance notes
The pattern is generally efficient due to limited quantifiers and alternations. However, the optional groups and quantifiers could lead to backtracking if the input is malformed. Anchoring at both ends ensures no partial matches. Consider simplifying if strict validation is required.