#分组能满足一些特定得需求 #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']#只分割一次
正则表达式三
猜你喜欢
转载自blog.csdn.net/qq_37550440/article/details/84024793
今日推荐
周排行