匹配字符

匹配单个字符

#匹配数字\d

str_new="123ada"
ret1=re.match("\d*3",str_new)
print(ret1.group())

匹配非数字\D

str_new="123ada"
ret2=re.match("\D*3",str_new)
try:
    print("匹配数据=",ret2.group())
except:
    print("没有匹配到数据")

匹配多次

  • 匹配0 次或多次

  •  ret5=re.match("asd*","asdddddd")
     
     try:
         print("匹配数据=",ret5.group())
     except:
         print("没有匹配到数据")
    

匹配1次或多次

  • 匹配1次或多次
ret=re.match("g+","good")
print(ret.group())

ret=re.match("g+","good")
print(ret.group())

? 匹配0次或1次

#?  匹配0次或1次
ret=re.match("g?","sd")
print(ret.group())

ret=re.match("g?","gg")
print(ret.group())

{n,m}匹配区间

ret=re.match("h[t]{1,6}","httttttttttttttps")
print(ret.group())

8.3.2.5 {n} 匹配指定次数

{n} 匹配指定次

ret5=re.match("[asd]{4}","asdasdasd")
try:
    print("匹配数据=",ret5.group())
except:
    print("没有匹配到数据")

8.3.2.6 {n,} 匹配n至无穷

{n,} 贪婪匹配

ret=re.match("h[t]{6,}","httttttttttttttps")
print(ret.group())

8.3.2.7匹配以XXX开头XXX结尾,除了XXX

ret=re.match("^1[3456789][0-9]{8}[^9]","16475858679")
print(ret.group())

猜你喜欢

转载自blog.csdn.net/qq_44090577/article/details/89951674