Python正则RE

Posted on 2019-12-19 12:06  zhuyu123  阅读(361)  评论(0编辑  收藏  举报

import re
s='adfddd19-1-084k12cd41jlv1999-1-28'
patten='\d{2,4}-\d{1,2}-\d{1,2}'

re1=re.findall(patten,s)#任意位置
print(re1)

re2=re.match(patten,s)#开头匹配
print(re2)

re3=re.search(patten,s)#匹配第一个
print(re3.group())

patten2='(\d{2,4})-(\d{1,2})-(\d{1,2})'
re4=re.search(patten2,s)#匹配第一个
print('year:',re4.group(1),'month:',re4.group(2),'day:',re4.group(3))

patten3='[a-z]+'#匹配任意字母
re5=re.findall(patten3,s)
print(re5)

patten4='[a-zA-Z][a-zA-Z0-9_]{3,4}$'#匹配字母开头,后续字母数字下划线,4-5位
re6=re.findall(patten4,s)
print(re6)
patten5='\d+\d*'
re7=re.findall(patten5,s)#匹配数字
print(re7)

Copyright © 2024 zhuyu123
Powered by .NET 9.0 on Kubernetes