检测电话号码的python程序(一)

时间:2015-05-14 13:40:45   收藏:0   阅读:792
def isPhoneNumber(text):
    #固定电话
    if len(text) != 13:
        return False  # 位数不够
    for i in range(0, 4):
        if not text[i].isdecimal():
            return False  # 区号不对
    if text[4] != ‘-‘:
        return False  # 没有分割符号
    for i in range(5, 13):
        if not text[i].isdecimal():
            return False  # 检测电话号码主体是不是数字
    return True  # "text" is a phone number!

print(‘0579-56574828 is a phone number:‘)
print(isPhoneNumber(‘0579-56574828‘))
print(‘Moshi moshi is a phone number:‘)
print(isPhoneNumber(‘Moshi moshi‘))

  

原文:http://www.cnblogs.com/ifconfig/p/4503039.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!