分享

python正则表达式findall验证座机和手机号

 大傻子的文渊阁 2020-02-14
def detect_legal_phone(phone_number):
    if phone_number == None or phone_number == "":
        return False
    if len(phone_number) != 11:
        return False
    for c in phone_number:
        if ord(c) >= ord("0") and ord(c) <= ord("9"):
            pass
        else:
            return False
    return True
def detect_phone(phone_number):
    """
    判断手机号
    :param phone_number:
    :return:
    """
    if phone_number == None or phone_number == "":
        return False
    if len(phone_number) != 11:
        return False
    regexp = "^\d{11}$"
    ret = re.findall(regexp, phone_number)
    if len(ret) == 0:
        return False
    return True
def detect_landline(landline_number):
    """
    判断座机
    :param landline_number:
    :return:
    """
    if landline_number == None or landline_number == "":
        return False
    regexp = "^0\\d{2,3}-\\d{7,8}$"
    ret = re.findall(regexp, landline_number)
    if len(ret) == 0:
        return False
    return True
import re
# 区号是4+7 或者3+8格式
regexp="^0\\d{2,3}-\\d{7,8}$"
ret = re.findall(regexp, "0577-56696365")
print(ret)
ret = re.findall(regexp, "010-85795231")
print(ret)
ret = re.findall(regexp, "01085795231")
print(ret)
ret = re.findall(regexp, "我企鹅王企鹅去玩01085795231谢谢谢谢谢谢是")
print(ret)
# ['0577-56696365']
# ['010-85795231']
# []
# []
# 简单判断手机号
regexp="^\d{11}$"
ret = re.findall(regexp, "15010127229")
print(ret)
ret = re.findall(regexp, "010-85795231")
print(ret)
ret = re.findall(regexp, "015010117229")
print(ret)
ret = re.findall(regexp, "我企鹅王企鹅去玩15010127229谢谢谢谢谢谢是")
print(ret)
# ['15010127229']
# []
# []
# []
ret = detect_phone("18867781008")
print(ret)
ret = detect_phone("11111111111")
print(ret)
ret = detect_phone("sdkjsfjksdbfsdfsdfsf")
print(ret)
ret = detect_landline("010-85795231")
print(ret)
ret = detect_landline("0577-56696365")
print(ret)
ret = detect_landline("sdkjsfjksdbfsdfsdfsf")
print(ret)
# True
# True
# False
# True
# True
# False

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约