Created
April 19, 2014 21:20
-
-
Save fengs/11097863 to your computer and use it in GitHub Desktop.
Parse American phone numbers, with optional extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
phonePattern = re.compile(r''' | |
(\d{3}) #area code | |
\D* | |
(\d{3}) #trunk | |
\D* | |
(\d{4}) #number | |
\D* | |
(\d*) #optional extension | |
$ | |
''', re.VERBOSE) | |
print phonePattern.search('800-810-6666-1234').groups() | |
print phonePattern.search('800.810.6666').groups() | |
print phonePattern.search('1-(800).810.6666').groups() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment