python3
re模块
1、匹配数字:^[0-9]*$^表示匹配字符串的开始位置,[0-9]表示匹配0~9中的任意一个数字,*匹配前一个字符0次或无数次,$匹配字符的结束位置。总的来说就是用于匹配数字的,该数字可以是2,也可以是22222222;具体代码如下:import rem=re.search('^[0-9]*$','222222222222')print('只匹配数字:',m.group())n=re.search('^[0-9]*$','111aaa')print('不能包括其它符号:',n.group())
2、匹配n位数字:^\d{n}$具体代码如下:import renum=re.findall('^\d{5}$','25255')print('匹配几位数字:',num)
3、匹配至少n位数字:^\d{n,}$具体代码如下:import rem=re.findall('^\d{3,}$','111222')print('匹配至少大于3位数字:',m)n=re.findall('^\d{8,}$','111222')print('匹配至少大于8位数字:',n)
4、匹配m~n的数字:^\d{m,n}$具体代码如下:import rem=re.findall('^\d{1,6}$','112233')print('匹配1-6位数字:',m)
5、匹配最多带2位小数点的数字:^([1-9][0-9]*)+(.[0-9]{1,2})?$具体代码如下:import rem=re.findall('^([1-9][0-9]*)+(.[0-9]{1,2})?$','112233.55')print('匹配最多带2位小数点的数字:',m)
6、匹配非0的正实数:具体代码如下:import rem=re.findall('^[1-9]\d*$','112233')print('匹配非0的正实数:',m)
7、汉字的匹配具体代码如下:import rem='my name is 张三'n=re.findall('[\u4e00-\u9fa5]{1,3}',m)print('匹配汉字:',n)
8、英文和数字的匹配具体代码如下:import rem='我的名字叫张三,今年18岁,Hai!!'n=re.findall('[A-Za-z0-9]+',m)print('匹配英文和数字:',n)p=re.findall('[A-Za-z]+',m)print('只匹配英文字符:',p)q=re.findall('[A-Z]+',m)print('只匹配英文大写字符:',q)x=re.findall('[a-z]+',m)print('只匹配英文小写字符:',x)y=re.findall('[A-Z0-9]+',m)print('只匹配英文大写字符和数字:',y)
9、中文、英文、数字和某些字符的匹配匹配有数字、26个英文字母户下划线组成的字符串:\w+匹配中文、英文、数字、下划线:[\u4e00-\u9fa5A-Za-z0-9_]+匹配中文、英文、数字:[\u4e00-\u9fa5A-Za-z0-9]+匹配输入含有^%&',;=?$\等字符的表达式:[^%&',;=?\x22]+具体代码如下:import rem='我的名字叫张三,今年18岁,我的手机号:86-,我的邮箱:zhang_san@qq.com,Hai!!'n=re.findall('\w+',m)print('匹配有数字、26个英文字母户下划线组成的字符串:',n)p=re.findall('[\u4E00-\u9FA5A-Za-z0-9_]+',m)print('匹配中文、英文、数字、下划线:',p)q=re.findall('[\u4e00-\u9fa5A-Za-z0-9]+',m)print('只匹配中文、英文、数字:',q)x=re.findall('[^%&,;=?\x22]+',m)print('匹配输入含有^%&,;=?$\特殊字符:',x)
如果这条经验帮到了您或对您有用,亲们可以点个赞或者关注一下小编,thanks!