正则表达式就是一种字符串的匹配规则
正则表达式的应用场景:
①数据验证
②数据检索
③数据过滤
import re
str1='6hello'
#使用match方法进行匹配,match()方法有限制,只能匹配字符串的第一个字符\d代表0-9的任意数字
result=re.match('\d',str1)
#使用group()输出正则匹配的结果
print(result.group())
#findall()可以匹配所有满足正则的结果,返回的是一个列表
'''
正则表达式三步走:
1、明确知道自己要查什么
2、明确知道我们具体要查多少个字符
3、明确我们在查询过程中从哪里开始,到哪里结束,
'''
#.点 查询任意字符
str2='8hello'
result=re.match('.',str2)
print(result.group())
#查询0-9
str3='3hello'
result=re.match('[0-9]',str3)
print(result.group())
#查询a-z
str4='hello'
result=re.match('[a-z]',str4)
print(result.group())
#查询A-Z
str5='Hhello'
result=re.match('[A-Z]',str5)
print(result.group())
#查询非数字
str6='gghello'
result=re.match('\D',str6)
print(result.group())
#查询空格
str7=' 3hello'
result=re.match('\s',str7)
print(result.group())
print(len(result.group()))
#查询非空格
str8='khello'
result=re.match('\S',str8)
print(result.group())
print(len(result.group()))
#查询非特殊字符,匹配汉字,字母,下划线数字
str9='嗨hello'
result=re.match('\w',str9)
print(result.group())
#查询特殊字符
str10='&hello'
result=re.match('\W',str10)
print(result.group())
^ 托字节:代表从哪个字符开始查起
$ 美元符号 : 代表查询到哪个字符结束
#匹配连续3位的数字
str1 = 'abc123def4567ghi'
result = re.findall('\d{3}', str1)
print(result)
#匹配数字出现1次或者多次,连续
str2='abc123457890def'
result=re.findall('\d+',str2)
print(result)
#匹配数字最多匹配一个,要么没有
str2='abc123457890def'
result=re.findall('\d+?',str2)
print(result)
#匹配数字出现最少1位到最多3位
str2='abc1234567890def'
result=re.findall('\d{1,3}',str2)
print(result)
#匹配第一位是1、第二位是3-9,后9位是连续的数字
str2='abc13458135123465dd15458354625425def'
result=re.findall('1[3456789]\d{9}',str2)
print(result)