python 正则表达式三

#分组能满足一些特定得需求
#re.match无分组
test = "good morning this is time 10"
test1 = re.match("g\w+",test)
print(test1.group()) #good #返回匹配的所有结果
print(test1.groups()) #()#获取模型中匹配到的分组结果
print(test1.groupdict()) #()#获取模型中匹配到分组中所有执行key的组
# #re.match有分组()
test = "good morning this is time 10"
test1 = re.match("g(?P<m1>\w+)",test)
print(test1.group())  #good
print(test1.groups()) #('g', 'ood')
print(test1.groupdict()) #{'m1': 'ood'}

#re.serch无分组
test = "good morning this is time 10"
test1 = re.search("m\w+",test)
print(test1.group()) #morning
print(test1.groups())
print(test1.groupdict())
#re.serch有分组
test = "good morning this is time 10"
test1 = re.search("m(\w+).*(?P<n1>\d)$",test)
print(test1.group()) #morning this is time 10
print(test1.groups()) #('orning', '0')
print(test1.groupdict()) #{'n1': '0'}

#findall,fileiter.split
test = "good morning this is time 10"
test1 = re.findall("t(\w+)",test)
print(test1)    #['his', 'ime']
test1 = re.findall("(t)(\w+)",test)
print(test1)    #[('t', 'his'), ('t', 'ime')]
test1 = re.findall("(t)((\w+)(i))(s)",test) #[('t', 'hi', 'h', 'i', 's')]
print(test1)    #[('t', 'hi', 'h', 'i', 's')]
test2 = re.finditer("m(\w+)(ng)",test)
for i in test2:
    print(i.group(),i.groups(),i.groupdict())
print(re.split("m\w+",test)) #['good ', ' this is ti', ' 10']
print(re.split("(m\w+)",test)) #['good ', 'morning', ' this is ti', 'me', ' 10']
print(re.split("(m\w+)",test)) #['good ', 'morning', ' this is ti', 'me', ' 10']#只分割一次
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流氓也是种气质 _Cookie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值