Skip to content

Instantly share code, notes, and snippets.

@fengs
Created April 19, 2014 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fengs/11097863 to your computer and use it in GitHub Desktop.
Save fengs/11097863 to your computer and use it in GitHub Desktop.
Parse American phone numbers, with optional extension
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