import re
reg1 = "^(133|149|153|173|177|180|181|189|199)+\d{8}$"
reg2 = "^(130|131|132|145|155|156|166|175|176|185|186)+\d{8}$"
reg3 = "^(134|135|136|137|138|139|147|150|151|152|157|158|159|178|182|183|184|187|188|198)+\d{8}$"
reg31 = "^134(0-8){8}$"
reg4 = "^(1700|1701|1702)+\d{7}$"
reg5 = "^(1703|1705|1706)+\d{7}$"
reg6 = "^(1704|1707|1708|1709|171\d)+\d{7}$"
def check_phone(str):
if len(re.findall(reg1, str)):
print("{} 中国电信".format(str))
return True
elif len(re.findall(reg2, str)):
print("{} 中国联通".format(str))
return True
elif len(re.findall(reg3, str)) or len(re.findall(reg31, str)):
print("{} 中国移动".format(str))
return True
elif len(re.findall(reg4, str)):
print("{} 虚拟运营商-电信".format(str))
return True
elif len(re.findall(reg5, str)):
print("{} 虚拟运营商-移动".format(str))
return True
elif len(re.findall(reg6, str)):
print("{} 虚拟运营商-联通".format(str))
return True
print("{} 非法".format(str))
return False
import random
def get_phone_number():
phone_number = '1'
for i in range(10):
n = random.randint(0, 9)
phone_number += str(n)
return phone_number
for i in range(100):
phone_number = get_phone_number()
check_phone(phone_number)
规则参考https://blog.csdn.net/lz3301413033/article/details/97755240