Python3正则匹配re.split,re.finditer及re.findall函数用法详解
本文实例讲述了Python3正则匹配re.split,re.finditer及re.findall函数用法。分享给大家供大家参考,具体如下:
re.splitre.finditerre.findall
@(python3)
官方re模块说明文档
re.compile()函数
编译正则表达式模式,返回一个对象。可以把常用的正则表达式编译成正则表达式对象,方便后续调用及提高效率。
re模块最离不开的就是re.compile函数。其他函数都依赖于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.split函数
按照指定的pattern格式,分割string字符串,返回一个分割后的列表。
re.split(pattern,string,maxsplit=0,flags=0)
- patterncompile生成的正则表达式对象,或者自定义也可
- string要匹配的字符串
- maxsplit指定最大分割次数,不指定将全部分割
importre str='sayhelloworld!hellopython' str_nm='one1two2three3four4' pattern=re.compile(r'(?P\s)')#创建一个匹配空格的正则表达式对象 pattern_nm=re.compile(r'(?P \d+)')#创建一个匹配空格的正则表达式对象 match=re.split(pattern,str) match_nm=re.split(pattern_nm,str_nm,maxsplit=1) print(match) print(match_nm)
结果:
['say','','hello','','world!','','hello','','python']
['one','1','two2three3four4']
re.findall()方法
返回一个包含所有匹配到的字符串的列表。
- pattern匹配模式,由re.compile获得
- string需要匹配的字符串
importre str='sayhelloworld!hellopython' pattern=re.compile(r'(?Ph\w)(?P l+)(?P o\s)')#分组,0组是整个world!,1组or,2组ld! match=re.findall(pattern,str) print(match)
结果
[('he','ll','o'),('he','ll','o')]
re.finditer、re.findall
re.finditer(pattern,string[,flags=0])
re.findall(pattern,string[,flags=0])
- patterncompile生成的正则表达式对象,或者自定义也可
- string要匹配的字符串
findall返回一个包含所有匹配到的字符的列表,列表类以元组的形式存在。
finditer返回一个可迭代对象。
示例一:
pattern=re.compile(r'\d+@\w+.com')#通过re.compile获得一个正则表达式对象 result_finditer=re.finditer(pattern,content) print(type(result_finditer)) print(result_finditer)#finditer得到的结果是个可迭代对象 foriinresult_finditer:#i本身也是可迭代对象,所以下面要使用i.group() print(i.group()) result_findall=re.findall(pattern,content) print(type(result_findall))#findall得到的是一个列表 print(result_findall) forpinresult_finditer: print(p)
输出结果:
123456@163.com
234567@163.com
345678@163.com
['123456@163.com','234567@163.com','345678@163.com']
由结果可知:finditer得到的是可迭代对象,finfdall得到的是一个列表。
示例二:
importre content='''email:123456@163.com email:234567@163.com email:345678@163.com ''' pattern=re.compile(r'(?P\d+)@(?P \w+).com') result_finditer=re.finditer(pattern,content) print(type(result_finditer)) print(result_finditer) iter_dict={}#把最后得到的结果 foriinresult_finditer: print('邮箱号码是:',i.group(1),'邮箱类型是:',i.group(2)) number=i.group(1) mail_type=i.group(2) iter_dict.setdefault(number,mail_type)#使用dict.setdefault创建了一个字典 print(iter_dict) print('+++++++++++++++++++++++++++++++') result_findall=re.findall(pattern,content) print(result_findall) print(type(result_findall))
输出结果:
邮箱号码是:123456邮箱类型是:163
邮箱号码是:234567邮箱类型是:163
邮箱号码是:345678邮箱类型是:163
{'123456':'163','234567':'163','345678':'163'}
+++++++++++++++++++++++++++++++
[('123456','163'),('234567','163'),('345678','163')]
finditer得到的可迭代对象i,也可以使用lastindex,lastgroup方法。
print('lastgroup最后一个被捕获的分组的名字',i.lastgroup)
findall当正则没有分组,返回就是正则匹配。
re.findall(r"\d+@\w+.com",content) ['2345678@163.com','2345678@163.com','345678@163.com']
有一个分组返回的是分组的匹配
re.findall(r"(\d+)@\w+.com",content) ['2345678','2345678','345678']
多个分组时,将结果作为元组,一并存入到列表中。
re.findall(r"(\d+)@(\w+).com",content) [('2345678','163'),('2345678','163'),('345678','163')]
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript
正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg
更多关于Python相关内容可查看本站专题:《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。