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.
^(\(\d{3}\) ?|\d{3}-)?\d{3}-\d{4}( x\d{1,5})?$
Copier
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