# 网站新用户注册页面获取手机号"""
校验规则要求必须是11位的纯数字,而且必须手机号必须是13, 15, 17, 18, 19开头
"""# python代码可以实现手机校验功能# 第一步:获取用户的手机号
new_phone_num = input('Deer customers,please enter your phone number>>>:').strip()
# 第二步:先校验是否是11位iflen(new_phone_num) == 11:
# 第三步:再校验是否是纯数字if new_phone_num.isdigit():
# 第四步:最后校验开头是否符合规则if new_phone_num.startswith('13') or new_phone_num.startswith('15') or new_phone_num.startswith('17') or new_phone_num.startswith('18') or new_phone_num.startswith('19'):
print('手机号检验成功')
else:
print('手机号开头不符合规则')
else:
print('手机号必须是纯数字')
else:
print('手机号必须是11位')
# 使用正则表达式import re
new_phone_number = input('please enter your phone number>>>:').strip()
if re.match('^(13|14|15|18|19)[0-9]{9}$', new_phone_number):
print('手机号检验成功')
else:
print('手机号不符合规则')
正则表达式之字符组
[0-9]
匹配0到9之间的任意一个数字(简写)
[a-z]
匹配a到z之间的任意一个小写字母
[0-9a-zA-Z]
匹配任意一个数字或者大小写字母(没有顺序)
[0123456789]
匹配0到9之间的任意一个数字
正则表达式之特殊符号
特殊符号
作用
.
匹配除换行符以外的任意字符
\w
匹配数字、字母、下划线
\d
匹配任意的数字
\t
匹配一个制表符(tab键)
^
匹配字符串的开始
$
匹配字符串的结尾
\W
匹配非字母或数字或下划线
\D
匹配非数字
a|b
匹配a或者b,管道符就是or
()
给正则表达式分组 ,不影响正则匹配
[]
字符组的概念
[^]
上箭号出现在了中括号的里面意思是取反操作
正则表达式之量词
特殊符号
作用
*
重复零次或者多次(默认就是多次:越多越好)
+
重复一次或者多次(默认就是多次:越多越好)
?
重复零次或者一次(默认就是一次:越多越好)
重复n次
重复最少n次最多多次(越多越好)
重复n到m次(越多越好)
复杂正则的编写
校验用户身份证号码
import re
# 身份证号码是一个长度为15或18个字符的字符串,如果是15位则全部由数字组成,首位不能为0;如果是18位,首位不能同样为0,前17位全部是数字,末位可能是数字或x
id_num = input('please enter you ID number>>>:')
if re.match('^[1-9][0-9]{14}'or'^[1-9][0-9]{16}[0-9x', id_num):
print('校验成功')
else:
print('号码不符合身份证规范')
校验顺丰快递单号
import re
SF_express_num = input('please enter your express number>>>:')
if re.match('^SF\d{13}$', SF_express_num):
print('校验成功')
else:
print('号码不符合规范')
取消转义
\n \n False
\\n \n True
\\\\n \\n True
贪婪匹配与非贪婪匹配
量词默认都是贪婪匹配,如果想修改为非贪婪匹配,只需要在量词的后面加?
正则
待匹配的文本
结果
<.*>
<s_cript>qwer1234<s_cript>
1条,贪婪匹配
<.*?>
<s_cript>qwer1234<s_cript>
2条,非贪婪匹配
re模块
import re
res = re.findall('e', 'Welcome to China')
print(res) # ['e', 'e']
res1 = re.search('e', 'Welcome to China')
print(res1) # <_sre.SRE_Match object; span=(1, 2), match='e'>print(res1.group()) # e
res2 = re.match('W', 'Welcome to China')
print(res2) # <_sre.SRE_Match object; span=(0, 1), match='W'>print(res2.group()) # W
res3 = re.finditer('e', 'Welcome to China')
print(res3) # <callable_iterator object at 0x000001F9AC99A320>print([obj.group() for obj in res3]) # ['e', 'e']
obj = re.compile('\d+?')
print(re.findall(obj, 'qwer123uiop789')) # ['1', '2', '3', '7', '8', '9']print(re.findall(obj, 'qazwsx147258bhunj8520imkolp369')) # ['1', '4', '7', '2', '5', '8', '8', '5', '2', '0', '3', '6', '9']
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?