Qaynaq LABS · TOOLS

REGEX · PYTHON

Regex explained

^\+?[1-9]\d{1,14}$

Matches a string representing a phone number starting with an optional '+' followed by a digit from 1-9 and 1 to 14 additional digits, with no leading zeros or other characters.

Token-by-token breakdown

TokenMeaningExample
^ 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
  • +123456789012345
  • 9
  • +9

Does not match

  • 0123456789012345
  • 1234567890123456
  • +0123456789012345
  • 123456789012345a
  • ++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.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Why is the first digit restricted to 1-9?

To avoid leading zeros, which are not valid in standard phone number representations.

Does this pattern validate international phone numbers?

No, it only validates a basic numeric format with optional '+'. It does not account for country codes, area codes, or other phone number conventions.

Can this pattern match empty strings?

No, because [1-9] requires at least one digit, and the quantifier {1,14} ensures at least one digit follows.

Is this pattern suitable for validating E.164 phone numbers?

Partially. E.164 numbers can be up to 15 digits, and this pattern allows 1-15 digits (1-9 followed by 1-14 digits) with an optional '+', which aligns with E.164 format.