user11666514
user11666514

Reputation: 175

Python Regex match phone Number

I have a regex that trying to match these phone number format. I'm super close except 1 scenario:

Regex: ^\(?(\d{3})\)?[-\. ]?(\d{3})[-\. ]?(\d{4})$

Phone number format are trying to match:

I also want to match this.... there is leading spaces before and after the area code:

[empty spaces] (123) [emtpy spaces] 456-7890

Upvotes: 0

Views: 117

Answers (1)

MyNameIsCaleb
MyNameIsCaleb

Reputation: 4489

^\s*\(?(\d{3})\)?[-\. ]*(\d{3})[-. ]?(\d{4})$

This should do it.

The * character means 0 or more of preceding.

Regexr is probably the best tool I know to figure out how to change regex patterns.

Upvotes: 3

Related Questions