twilio_calls.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:h4h_2017 作者: flyrightsister 项目源码 文件源码
def parse_phone(phone_num):
    """Takes a phone number in a variety of formats and returns 10 digits.

    arguments: 
        phone_num: string containing a phone number, in one of these formats: 
            (555) 555-5555
            (555)555-5555
            555-555-5555
            5555555555

    returns:
        string of 10 digits (neglecting errors for now), or None if error.

    Examples / doctests:

        >>> print parse_phone("(555) 555-5555")
        5555555555

        >>> print parse_phone("(555)555-5555")
        5555555555

        >>> print parse_phone("555-555-5555")
        5555555555

        >>> print parse_phone("555555-5555")
        5555555555

        >>> print parse_phone("(555) 555-55555")
        None

    """

    # a somewhat obscure regular expression to get the data out of the phone
    # number in various formats. (see http://regex101.com for more details on 
    # -- and a sandbox for -- regular expressions.)
    matches = re.match(r'^\(?(\d{3})\)?[\s\-]?(\d{3})-?(\d{4})$', phone_num)

    if not matches: 
        # the phone number wasn't in one of the acceptable formats
        return None

    # get the data from the regular expression
    # for more details, see 
    # https://docs.python.org/2/library/re.html#match-objects
    area_code = matches.group(1)
    exchange = matches.group(2)
    other_part = matches.group(3)
    return "{}{}{}".format(area_code, exchange, other_part)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号