I need to match to a NANP standard 10-digit phone number with some using – as a separator (e.g. 123-456-7890) and some will have a dot (.) as a separator (e.g. 123.456.7890).
I have tried:
numbers = "123-456-7890, 123.456.7890"
phones = re.findall("\d{3}*\D*\d{3}*\D*\d{4}", numbers)
CodePudding user response:
Try this:
import re
result = re.findall('\d{3}[-.]\d{3}[-.]\d{4}', numbers)
print(result)
Output: ['123-456-7890', '123.456.7890']
[-.]
: match only-
or.
\d{3}
:three
times match any digit(0-9)