Regular Expressions 101

Community Patterns

Validation of Phone Numbers including optional country code and area code

0

Regular Expression
Python

r"
^\(?\+?([0-9]{1,3})?[- (]?\(?([0-9]{3})?[-. )]*?\(?([0-9]{3})[-. )]*?\(?([0-9]{4})\)?\s?$
"
gm

Description

numbers = ['(210) 867 5309', '+1 210.867.5309', '867-5309', '210-867-5309'] regex = r"^(?+?([0-9]{1,3})?[- (]?(?([0-9]{3})?[-. )]?(?([0-9]{3})[-. )]?(?([0-9]{4}))?\s?$" re.search(regex, phone_number)

Submitted by Cristina Lucin - a year ago