"""Parse U.S. phone numbersThis program is part of "Dive Into Python", a free Python book forexperienced programmers. Visit http://diveintopython.org/ for thelatest version."""__author__="Mark Pilgrim (mark@diveintopython.org)"__version__="$Revision: 1.3 $"__date__="$Date: 2004/05/05 21:57:19 $"__copyright__="Copyright (c) 2002 Mark Pilgrim"__license__="Python"importreclassInvalidPhoneNumber(Exception):passphonePattern=re.compile(r""" ^ # match beginning of string \D* # swallow anything that isn't numeric 1? # swallow leading 1, if present \D* # swallow anything that isn't numeric (\d{3}) # capture 3-digit area code \D* # swallow anything that isn't numeric (\d{3}) # capture 3-digit trunk \D* # swallow anything that isn't numeric (\d{4}) # capture 4-digit number \D* # swallow anything that isn't numeric (\d*) # capture extension, if present """,re.VERBOSE)defparsePhoneNumber(phoneNumber):match=phonePattern.search(phoneNumber)ifmatch:returnmatch.groups()else:raiseInvalidPhoneNumber,phoneNumber