Python (re)

International Phone Number (Loose) in PY

Match international phone numbers in a variety of loose formats including country codes, area codes, and separators.

Try it in the PY tester →

Pattern

regexPY
\+?[1-9]\d{0,3}[\s.\-]?(?:\(?\d{1,4}\)?[\s.\-]?){2,4}\d{1,9}   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"\+?[1-9]\d{0,3}[\s.\-]?(?:\(?\d{1,4}\)?[\s.\-]?){2,4}\d{1,9}")
input_text = "+1 (415) 555-2671"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

\+? optionally matches a leading +. [1-9]\d{0,3} matches 1–4 digit country/area code. The repeating group (?:\(?\d{1,4}\)?[\s.\-]?){2,4} matches digit groups with optional parentheses and separators. Ends with 1–9 final digits.

Examples

Input

+1 (415) 555-2671

Matches

  • +1 (415) 555-2671

Input

+44 20 7183 8750

Matches

  • +44 20 7183 8750

Input

not a phone

No match

Same pattern, other engines

← Back to International Phone Number (Loose) overview (all engines)