Python3中正则模块re.compile、re.match及re.search函数用法详解
本文实例讲述了Python3中正则模块re.compile、re.match及re.search函数用法。分享给大家供大家参考,具体如下:
re模块re.compile、re.match、re.search
re模块官方说明文档
正则匹配的时候,第一个字符是r,表示rawstring原生字符,意在声明字符串中间的特殊字符不用转义。
比如表示‘\n',可以写r'\n',或者不适用原生字符‘\n'。
推荐使用re.match
re.compile()函数
编译正则表达式模式,返回一个对象。可以把常用的正则表达式编译成正则表达式对象,方便后续调用及提高效率。
re.compile(pattern,flags=0)
- pattern指定编译时的表达式字符串
- flags编译标志位,用来修改正则表达式的匹配方式。支持re.L|re.M同时匹配
flags标志位参数
re.I(re.IGNORECASE)
使匹配对大小写不敏感
re.L(re.LOCAL)
做本地化识别(locale-aware)匹配
re.M(re.MULTILINE)
多行匹配,影响^和$
re.S(re.DOTALL)
使.匹配包括换行在内的所有字符
re.U(re.UNICODE)
根据Unicode字符集解析字符。这个标志影响\w,\W,\b,\B.
re.X(re.VERBOSE)
该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解。
示例:
importre content='Citizenwang,alwaysfallinlovewithneighbour,WANG' rr=re.compile(r'wan\w',re.I)#不区分大小写 print(type(rr)) a=rr.findall(content) print(type(a)) print(a)
findall返回的是一个list对象
['wang','WANG']
re.match()函数
总是从字符串‘开头曲匹配',并返回匹配的字符串的match对象
re.match(pattern,string[,flags=0])
- pattern匹配模式,由re.compile获得
- string需要匹配的字符串
importre pattern=re.compile(r'hello') a=re.match(pattern,'helloworld') b=re.match(pattern,'worldhello') c=re.match(pattern,'hell') d=re.match(pattern,'hello') ifa: print(a.group()) else: print('a失败') ifb: print(b.group()) else: print('b失败') ifc: print(c.group()) else: print('c失败') ifd: print(d.group()) else: print('d失败')
hello
b失败
c失败
hello
match的方法和属性
参考链接
importre str='helloworld!hellopython' pattern=re.compile(r'(?Phell\w)(?P \s)(?P .*ld!)')#分组,0组是整个helloworld!,1组hello,2组ld! match=re.match(pattern,str) print('group0:',match.group(0))#匹配0组,整个字符串 print('group1:',match.group(1))#匹配第一组,hello print('group2:',match.group(2))#匹配第二组,空格 print('group3:',match.group(3))#匹配第三组,ld! print('groups:',match.groups())#groups方法,返回一个包含所有分组匹配的元组 print('start0:',match.start(0),'end0:',match.end(0))#整个匹配开始和结束的索引值 print('start1:',match.start(1),'end1:',match.end(1))#第一组开始和结束的索引值 print('start2:',match.start(1),'end2:',match.end(2))#第二组开始和结束的索引值 print('pos开始于:',match.pos) print('endpos结束于:',match.endpos)#string的长度 print('lastgroup最后一个被捕获的分组的名字:',match.lastgroup) print('lastindex最后一个分组在文本中的索引:',match.lastindex) print('string匹配时候使用的文本:',match.string) print('re匹配时候使用的Pattern对象:',match.re) print('span返回分组匹配的index(start(group),end(group)):',match.span(2))
返回结果:
group0:helloworld!
group1:hello
group2:
group3:world!
groups:('hello','','world!')
start0:0end0:12
start1:0end1:5
start2:0end2:6
pos开始于:0
endpos结束于:25
lastgroup最后一个被捕获的分组的名字:last
lastindex最后一个分组在文本中的索引:3
string匹配时候使用的文本:helloworld!hellopython
re匹配时候使用的Pattern对象:re.compile('(?Phell\\w)(?P \\s)(?P .*ld!)')
span返回分组匹配的index(start(group),end(group)):(5,6)
re.search函数
对整个字符串进行搜索匹配,返回第一个匹配的字符串的match对象。
re.search(pattern,string[,flags=0])
- pattern匹配模式,由re.compile获得
- string需要匹配的字符串
importre str='sayhelloworld!hellopython' pattern=re.compile(r'(?Phell\w)(?P \s)(?P .*ld!)')#分组,0组是整个helloworld!,1组hello,2组ld! search=re.search(pattern,str) print('group0:',search.group(0))#匹配0组,整个字符串 print('group1:',search.group(1))#匹配第一组,hello print('group2:',search.group(2))#匹配第二组,空格 print('group3:',search.group(3))#匹配第三组,ld! print('groups:',search.groups())#groups方法,返回一个包含所有分组匹配的元组 print('start0:',search.start(0),'end0:',search.end(0))#整个匹配开始和结束的索引值 print('start1:',search.start(1),'end1:',search.end(1))#第一组开始和结束的索引值 print('start2:',search.start(1),'end2:',search.end(2))#第二组开始和结束的索引值 print('pos开始于:',search.pos) print('endpos结束于:',search.endpos)#string的长度 print('lastgroup最后一个被捕获的分组的名字:',search.lastgroup) print('lastindex最后一个分组在文本中的索引:',search.lastindex) print('string匹配时候使用的文本:',search.string) print('re匹配时候使用的Pattern对象:',search.re) print('span返回分组匹配的index(start(group),end(group)):',search.span(2))
注意re.search和re.match匹配的str的区别
打印结果:
group0:helloworld!
group1:hello
group2:
group3:world!
groups:('hello','','world!')
start0:4end0:16
start1:4end1:9
start2:4end2:10
pos开始于:0
endpos结束于:29
lastgroup最后一个被捕获的分组的名字:last
lastindex最后一个分组在文本中的索引:3
string匹配时候使用的文本:sayhelloworld!hellopython
re匹配时候使用的Pattern对象:re.compile('(?Phell\\w)(?P \\s)(?P .*ld!)')
span返回分组匹配的index(start(group),end(group)):(9,10)
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript
正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg
更多关于Python相关内容可查看本站专题:《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。