1

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)
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
JD13
  • 43
  • 5
  • Does this answer your question? [How to validate phone numbers using regex](https://stackoverflow.com/questions/123559/how-to-validate-phone-numbers-using-regex) – ti7 Oct 29 '22 at 19:21
  • If you want to force the same separator by [capturing](https://www.regular-expressions.info/refcapture.html): [`\d{3}([.-])\d{3}\1\d{4}`](https://regex101.com/r/HuBge3/1) – bobble bubble Oct 29 '22 at 19:39

1 Answers1

2

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)
I'mahdi
  • 23,382
  • 5
  • 22
  • 30