python正则表达式findall验证座机和手机号
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