InRegex Logo
InRegex
Nuovo

Comprehensive US Phone Number Validator

Spiegazione

This regex pattern validates US phone numbers with the following format: (optional area code in parentheses or without, optional space), 3 digits, hyphen, 3 digits, hyphen, 4 digits, and an optional extension starting with 'x' followed by 1 to 5 digits.

python

Espressione Regolare

^(\(\d{3}\) ?|\d{3}-)?\d{3}-\d{4}( x\d{1,5})?$
Copia
python

Esempio di Utilizzo

import re

phone_number = '555-123-4567'
if re.match(r'^(\(\d{3}\) ?|\d{3}-)?\d{3}-\d{4}( x\d{1,5})?$', phone_number):
    print('Valid US phone number')
else:
    print('Invalid US phone number')
Copia

Espressioni Regolari Simili