parsephone.py

来自「Dive into Python 中文版」· Python 代码 · 共 38 行

PY
38
字号
"""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"import reclass InvalidPhoneNumber(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)def parsePhoneNumber(phoneNumber):    match = phonePattern.search(phoneNumber)    if match:        return match.groups()    else:        raise InvalidPhoneNumber, phoneNumber

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?