php程序员学习python3学习第十一天
正则表达式重新学习
#-*- coding:utf-8 -*- #正则表达式 import re # . 通配符 res = re.findall('haha.w','dsadsahahacwhaha(w') #一个点表示任意一个字符都可 print(res) # ^ 字符以什么开始 res = re.findall('^hello','hellohahah') #匹配得到以hello开头,返回hello print(res) # $ 以什么来进行结尾 res = re.findall('hello$','fefewfwhello') #匹配到以hello结尾返回hello print(res) # * 贪婪匹配,匹配其前一个字符0到多次 res = re.findall('hello*','sadsahello') print(res) #hello res = re.findall('hello*','sadsahell') print(res) # hell res = re.findall('hello*','dsasdhellooooo') print(res) #hellooooo # + 一到多次 res = re.findall('hello+','sadsahello') print(res) #['hello'] res = re.findall('hello+','sadsahell') print(res) #[] #? 匹配0次或者1次 res = re.findall('hello?','fdsfdshello') print(res) #['hello'] res = re.findall('hello?','dsafrwwhell') print(res) #['hell'] res = re.findall('hello?','fdsfsdhellooooo') print(res) #['hello'] #{} 匹配指定次数 res = re.findall('hello{1}','hello') print(res) #['hello'] res = re.findall('hello{0,2}','hellooo') #匹配0到2次 0,1,2 print(res) #['hell'] res = re.findall('hello{3}','hello') print(res) #[] #[] 可选字符集匹配 元字符在字符集中大部分都失去功能 res = re.findall('a[bc]d','wwwwabd') #abd,acd都可匹配 print(res) #['abd'] res = re.findall('a[bc]d','fdsddadd') print(res) #[] #[a-z] 匹配a到z都可 [1-9] 1到9都可 #[^1-9] 除了1到9都取到了 元字符^在字符集中作用是非的意思 #() 把括号中的内容看成一个组来进行匹配 print(re.findall("(ab)*","abcde")) #['ab','','',''] print(re.findall("(ab)*", "acde")) #['','','',''] print(re.search("(ab)*", "ababcd").group()) #abab print(re.search("(ab)","ababab").group()) #ab #\ 反斜线后跟元字符,元字符失去特殊意义,跟其他字母,附加特殊意义 ''' \d 匹配任何十进制数,相当于[0-9] \D 匹配任何非数字字符 相当于[^0-9] \s 匹配任何空白字符 相当于[\t\n\r\f\v] \S 匹配任何非空白字符 相当于[^\t\n\r\f\v] \w 匹配任何字母数字字符 相当于[a-zA-Z0-9] \W 匹配任何非字母数字字符 相当于[^a-zA-Z0-9] \b 匹配一个单词边界,也就是单词和空格直接的位置 ''' #非贪婪模式 ''' * + ?都是贪婪模式,因此在其后加一个?构成非贪婪模式 ''' print(re.findall("a(\d+?)","a23b")) # 分组,使用序号使用组中内容 \2 \1 print(re.search(r'a(haha)(wawa)com\2','ahahawawacomwawahaha').group()) #\2表示(wawa)中内容 #\b 匹配单词 print(re.search('abc\\b','abc ddffgg').group()) #函数的第三个参数,表示使用哪种方式来进行匹配,默认是0 ''' re.I 使匹配对大小写不敏感 re.L 做本地化识别(local-aeare) re.M 多行匹配 影响^和$ re.S 使.匹配包括换行在内的所有字符 re.U 根据unicode字符集解析字符,这个标志影响\w \W \b re.X 该标志通过给予你更灵活的格式以便你将正则表达式写的更 ''' #group方法,其中可以添加参数 #group(0) 默认即为0,取出所有组的内容 group(1) 只取第一个组的内容 group(2) # # ##正则表达式的替换使用re模块的sub方法 subn方法,可以知道替换了多少次 sub1 = 'I get a I get b I get c' res = re.sub('g.t', 'have', sub1) res1 = re.sub('g.t', 'have', sub1, 2) print(sub1) print(res) print(res1) res2 = re.subn('g.t', 'have', sub1) print(res2) #compile 将规则先编译到对象中,然后使用对象进行匹配 text = 'hahaoohaha hoohffff ffffff' regex = re.compile(r'\w*oo\w*') #匹配包含oo的单词 print(regex.findall(text)) #split() 将字符串进行分割 根据指定规则,先编译再进行分割 regex = re.compile(r'\d+') test = 'haha1bb2cc3dd4' print(regex.split(test)) #finditer() 匹配符合规则的字符及其所在位置的可遍历对象 p = re.compile(r'\d+') w = p.finditer('12 dsffwe333ewrwe fefewfew23 frfw11 11') for match in w: print(match.group(), match.span()) #group() groups() groupdict() str1 = "hello world python" r = re.match('h\w+',str1) print(r.group()) #hello 获取匹配到的所有结果 print(r.groups()) #() 获取模型中匹配到的分组结果 若规则为h(\w+),则匹配到了('ello',) print(r.groupdict()) #{} 获取模型中匹配到的分组中所有执行了key的组 str2 = "hello world python" r = re.match('(?P<A1>h)(?P<A2>\w+)',str2) print(r.group()) print(r.groups()) print(r.groupdict())
-------------------------立码平天下------------------------------

浙公网安备 33010602011771号