import re ret = re.findall('\d+', '15548jhbsbcvjsbcv1548') print(ret) ret = re.search('\d+', '15548jhbsbcvjsbcv1548') print(ret) #变量 print(ret.group()) """ split sub subn match compile finditer """ ret = re.split('\d+','aaaa222bbbb') print(ret) ret = re.split('(\d+)','aaaa222bbbb') print(ret) #sub 替换 ret = re.sub('\d+', 'H', 'aaaa222bbbb',1) print(ret) #subn 返回元组,说明替换次数 ret = re.subn('\d+', 'H', 'aaaa222bbbb555') print(ret) #match 要从头开始找,不然返回None,和seach前面加^一样 #match 一般用来规定这个字符号必须是什么样的,seach用来寻找这个字符串中是不是含有满足条件的子内容 ret = re.match('\d+', 'aaaa222bbbb555') print(ret) #compile 节省代码时间工具 #假如同一个正则表达式要被使用多次 #节省了多次解析同一个正则表达式的时间 ret = re.compile('\d+') print(ret.search('aaa25151')) print(ret.findall('aaa155151')) print("*************************") #finditer 节省空间 ret = re.finditer('\d+','fwdwfv15155') for i in ret: print(i.group()) print("*"*10) #先compile 再finditer ret = re.compile('\d+') res = ret.finditer('fdcafdag5151fewf51511') for r in res: print(r.group()) #分组命名 import re ret = re.search('\d(\d)\d(\w+)(?P<name1>\d)(\w)\d(\d)\d(\w+)(\d)(?P<name2>\w)\d(\d)\d(\w+)(\d)(\w)', '123abc45678sad_415ada12515fwefwe455wee415egwe') print(ret.group('name1')) print(ret.group('name2')) #分组命名的引用 exp = '<abc>ahgcgkh<abh>,kl,k</abc>' ret = re.search('<(?P<tag>\w+)>.*?</(?P=tag)>',exp) print(ret)
标签:finditer,group,ret,compile,re,模块,print From: https://www.cnblogs.com/moon3496694/p/17131599.html