python验证输入的手机是否为中国移动的号码

使用re模块,输入两个手机号码,进行验证:

import re
pattern = r'(13[4-9]\d{8})$|(15[01289]\d{8})$'
mobile = '13634222222'
match =re.match(pattern,mobile)
if match == None:
    print(mobile,'不是有效的中国移动手机号码')
else:
    print(mobile,'是有效的中国移动手机号码')
mobile = '13144222221'
match =re.match(pattern,mobile)
if match == None:
    print(mobile,'不是有效的中国移动手机号码')
else:
    print(mobile,'是有效的中国移动手机号码')

结果:

13634222222 是有效的中国移动手机号码
13144222221 不是有效的中国移动手机号码

猜你喜欢

转载自www.cnblogs.com/xiao02fang/p/12913175.html