python - 在 python 中命名为非捕获组?

标签 python regex

是否可以在 python 中命名非捕获组?例如,我想匹配此模式中的字符串(包括引号):

“a=b”
'鸟=天使'

我可以执行以下操作:

s = '"bird=angel"'
myre = re.compile(r'(?P<quote>[\'"])(\w+)=(\w+)(?P=quote)')
m = myre.search(s)
m.groups()
# ('"', 'bird', 'angel')

结果捕获了报价组,这在此处是不可取的。

最佳答案

不,命名组总是捕获组。来自 re 的文档模块:

Extensions usually do not create a new group; (?P<name>...) is the only exception to this rule.



关于命名组扩展:

Similar to regular parentheses, but the substring matched by the group is accessible within the rest of the regular expression via the symbolic group name name



在哪里 regular parentheses表示 (...) ,与 (?:...) 相比.

关于python - 在 python 中命名为非捕获组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16471776/

相关文章:

python - 在 python 中使用 OR 选项构建 grep 命令字符串

Python:只要调用 help ('modules' ),Kivy 就会自动启动

Python 将文件系统映射到目录结构 : works, ...但是如何呢?

Python 以 javascript 形式登录网站

python - 如何知道python中 turtle 图形上特定文本的像素大小?

Python正则表达式findall捕获重复组

Python正则表达式替换没有按我的预期工作

javascript - JS RegExp 中的\u007F 和\uFFFF

java - 当字符串被 (a-z) 包围时,从字符串中删除破折号,但不要从中间删除破折号

python - 更新pandas中特定行范围内的列值