第11.25节 Python正则表达式编译re.compile及正则对象使用

一、 引言
在《第11.2节 Python 正则表达式支持函数概览》介绍了re模块的主要函数,在《第11.3节 Python正则表达式搜索支持函数search、match、fullmatch、findall、finditer》重点介绍了几个搜索函数。这些介绍的搜索函数都是直接使用正则表达式去匹配搜索文本,实际上re模块还支持将正则表达式先编译再搜索匹配,这种先编译后搜索在同一个正则表达式多次去执行匹配时可以提高匹配执行效率。

二、 re.compile函数

  1. 语法:compile(pattern, flags=0)
  2. 语法释义:
    pattern是要匹配的正则表达式,flags是在《第11.2节 Python 正则表达式支持函数概览》介绍的搜索标记。
  3. 返回结果:
    返回结果是一个类型为re.Pattern的对象,该对象称为 “正则表达式对象”或“正则对象”。该对象可以直接使用自己的方法search、match等进行搜索和匹配。

三、 正则对象Pattern的属性

  1. Pattern.flags:正则匹配标记。这是可以传递给 compile() 的参数,任何 (?…) 内联标记,隐性标记比如 UNICODE 的结合。不过这里的值是一个数字,而不是使用标记的常量,数字与匹配标记常量对应关系请参考《第11.2节 Python 正则表达式支持函数概览》;
  2. Pattern.groups:捕获组合的数量;
  3. Pattern.groupindex:在命名组内记录组名和组序号的映射关系。如果没有命名组,那字典就是空的。
  4. Pattern.pattern:编译对象的对应的正则表达式。

四、 正则对象的方法

  1. Pattern.search(string[, pos[, endpos]])
    这个方法与re.search函数功能类似,扫描整个 string 寻找第一个匹配位置, 并返回一个相应匹配对象。如果没有匹配,就返回 None。不过比re.search函数多了两个可选参数,可选的第二个参数 pos 给出了字符串中开始搜索的位置索引,默认为 0,如果大于0,对于元字符 ‘^’ 和特殊序列A样式字符匹配字符串真正的开头,或换行符后面的第一个字符,但不会匹配索引规定开始的位置。
    可选参数 endpos 限定了字符串搜索的结束;当endpos比搜索文本长度小的时候,字符串末尾的匹配如’$’会匹配到endpos对应位置,也就是实际搜索文本变成了原搜索文本从开始到endpos位置切片对应的新搜索文本。

案例:

>>> str='Learning Python  with LaoYuan123'
>>> str[22:29]
'LaoYuan'
>>> patstr='LaoYuan$'
>>> pat=re.compile(patstr)
>>> pat.search(str,endpos=29)
<re.Match object; span=(22, 29), match='LaoYuan'>
>>>

上述案例中搜索文本结尾是123,但指定了结束位置刚好到搜索模式串,因此$最终匹配成功。

  1. Pattern.match(string[, pos[, endpos]])
    该方法与re.match相对应,功能相差不多,从 string 的 开始位置 能够找到这个正则样式的任意个匹配,就返回一个相应的 匹配对象。如果不匹配,就返回 None 。可选参数 pos 和 endpos 与 search() 含义相同。
    如:
>>> patstr='Python'
>>> str='LaoYuanPython'
>>> pat=re.compile(patstr)
>>> pat.match(str,7)
<re.Match object; span=(7, 13), match='Python'>
>>>

  1. Pattern.fullmatch(string[, pos[, endpos]])
    该方法与re.fullmatch()函数相对应,如果整个 string 匹配这个正则表达式,就返回一个相应的 匹配对象 。 否则就返回 None。可选参数 pos 和 endpos 与 Pattern.search() 含义相同。
    案例:
>>> pat=re.compile(r"第[一-十][章回](.)*")
>>> pat.fullmatch('第二回 悟彻菩提真妙理 断魔归本合元 ')
<re.Match object; span=(0, 19), match='第二回 悟彻菩提真妙理u3000断魔归本合元 '>
>>>
  1. Pattern.split(string, maxsplit=0)
    等价于 re.split 函数,只是使用了编译后的正则对象来调用。
    案例:
>>> pat=re.compile(r'(W+)')
>>> pat.split('Hello,world')
['Hello', ',', 'world']
>>>
  1. Pattern.findall(string[, pos[, endpos]])
    类似函数 re.findall函数,只是使用了正则对象来调用,比re.findall多了两个可选参数 pos 和 endpos用于限制搜索范围,这两个参数的作用与Pattern.search方法的这两个参数一样。
    案例:
>>> pat=re.compile(r'(w+)')
>>> pat.findall('Learning python with LaoYuan')
['Learning', 'python', 'with', 'LaoYuan']
>>>
  1. Pattern.finditer(string[, pos[, endpos]])
    类似函数re.finiter() , 使用了编译后的正则对象来调用,比re.finiter()多了两个可选参数 pos 和 endpos用于限制搜索范围,这两个参数的作用与Pattern.search方法的这两个参数一样。
    案例:
>>> pat=re.compile(r"第[一-十][章回]")
>>> for i in pat.finditer("第一回灵根育孕源流出 心性修持大道生 第二回悟彻菩提真妙理 断魔归本合元神"):print(i)

<re.Match object; span=(0, 3), match='第一回'>
<re.Match object; span=(19, 22), match='第二回'>
>>>
  1. Pattern.sub(repl, string, count=0)
    等价于 re.sub函数,使用了编译后的正则对象来调用。
    案例:
>>> patstr=r'(?i)(python)'
>>> pat=re.compile(patstr)
>>> pat.sub(r'1->g<1>->Python','Learning python with LaoYuan,LaoYuanPython accompanies you to progress')
'Learning python->python->Python with LaoYuan,LaoYuanPython->Python->Python accompanies you to progress'
>>>
  1. Pattern.subn(repl, string, count=0)
    等价于 re.subn() 函数,使用了编译后的正则对象来调用。
>>> pat.subn(r'1->g<1>->Python','Learning python with LaoYuan,LaoYuanPython accompanies you to progress')
('Learning python->python->Python with LaoYuan,LaoYuanPython->Python->Python accompanies you to progress', 2)
>>>

老猿Python,跟老猿学Python!
博客地址:https://blog.csdn.net/LaoYuanPython

请大家多多支持,点赞、评论和加关注!谢谢!

原文地址:https://www.cnblogs.com/LaoYuanPython/p/13643612.html