最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【教程】详解Python正则表达式之: (?…) extension notation 扩展助记符

Python re crifan 3988浏览 0评论

Python 手册的解释是:

(?...)
This is an extension notation (a '?' following a '(' is not meaningful otherwise). The first character after the '?' determines what the meaning and further syntax of the construct is. Extensions usually do not create a new group; (?P<name>...) is the only exception to this rule. Following are the currently supported extensions.

下面详细解释一下具体含义:

(?…) 是个,扩展助记符,其是一个完整的组合,此时才有真正的含义。

换句话说:

1. 如果只是一个左半圆括号后面跟着一个问号?,但是后面却没有右半圆括号,则前面的 左半圆括号和问号,是没有特殊含义的。只是普通的正则表达式中的字符而已。

2. 而关于本身(?…)这个,扩展助记符,完整的组合的含义,要取决于问号?后面的那个字符来决定。(具体包含哪些类型,下面会详细介绍)

而扩展助记符,一般来说,相对于之前所介绍的圆括号group:

【教程】详解Python正则表达式之: (…) group 分组

而言,group的圆括号会产生group,而此处,一般情况下并不会创建,即产生,新的组group,不过(?P<name>) 是个特列,即,(?P<name>)也会产生group。


下面就来介绍一下,(?…) 具体包含哪些类型:

【教程】详解Python正则表达式之: (?iLmsux) group match empty string using flag 通过标志匹配空字符的组

【教程】详解Python正则表达式之: (?:…) non-capturing group 非捕获组

【教程】详解Python正则表达式之: (?P<name>…) named group 带命名的组

【教程】详解Python正则表达式之: (?P=name) match earlier named group 匹配前面已命名的组

【教程】详解Python正则表达式之: (?#…) add comment 添加注释

【教程】详解Python正则表达式之: (?=…) lookahead assertion 前向匹配 /前向断言

【教程】详解Python正则表达式之: (?!…) negative lookahead assertion 前向否定匹配 /前向否定断言

【教程】详解Python正则表达式之: (?<=…)  positive lookbehind assertion 后向匹配 /后向断言

【教程】详解Python正则表达式之: (?<!…) negative lookbehind assertion 后向否定匹配/后向否定断言

【教程】详解Python正则表达式之: (?(id/name)yes-pattern|no-pattern) 条件性匹配

转载请注明:在路上 » 【教程】详解Python正则表达式之: (?…) extension notation 扩展助记符

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.164 seconds, using 22.11MB memory