1 实验题目
1.1 实验介绍
我们在注册应用的时候常常使用手机号作为账户名,在短信验证之前一般都会监测号码的真实性,如果是不存在的号码就不会发送验证码。
检验规则如下∶
- 长度不少于 11 位
- 是移动、联通、电信号段中的一个电话号码。
注意:因为是输入电话号码,输入除电话号码其他字符可以忽略。
CN_mobile = [134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705]
CN_union = [130,131,132,155,156,185,186,145,176,1709]
CN_telecom = [133,153,180,181,189,177,1700]
1.2 实验要求
编写程序,模检测输入电话号码的真实性。
2 实验原理
2.1 正则表达式基本语法


2.2 Python标准库re模块
Python标准库re模块提供了正则表达式操作所需要的功能。

3 运行结果

4 源代码
import re
telephone = input('请输入电话号码:')
mobile = '(134|135|136|137|138|139|150|151|\
152|157|158|159|182|183|184|187|188|\
147|178|1705)[1-9]+'
union = '(130|131|132|155|156|185|186|145|176|1709)[1-9]+'
telecom = '(13|153|180|181|189|177|1700)[1-9]+'
reality = False
if len(telephone) >= 11:
m = re.search(mobile, telephone)
u = re.search(union, telephone)
t = re.search(telecom, telephone)
if m or u or t:
reality = True
if reality:
print('该电话号码真实')
else:
print('该电话号码不存在')
1162

被折叠的 条评论
为什么被折叠?



