InRegex Logo
InRegex

Comprehensive US Phone Number Validator

Explication

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

Expression Régulière

^(\(\d{3}\) ?|\d{3}-)?\d{3}-\d{4}( x\d{1,5})?$
Copier
Avez-vous des Retours ?
python

Exemple d'Utilisation

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')
Copier

Expressions Régulières Similaires