Python 正则表达式 - 如何从通配符表达式中捕获多个组? [英] Python regular expressions - how to capture multiple groups from a wildcard expression?

查看:47
本文介绍了Python 正则表达式 - 如何从通配符表达式中捕获多个组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Python 正则表达式,其中包含一个可以出现零次或多次的组 - 但是当我之后检索组列表时,只有最后一个出现.示例:

I have a Python regular expression that contains a group which can occur zero or many times - but when I retrieve the list of groups afterwards, only the last one is present. Example:

re.search("(\w)*", "abcdefg").groups()

这将返回列表 ('g',)

this returns the list ('g',)

我需要它返回 ('a','b','c','d','e','f','g',)

I need it to return ('a','b','c','d','e','f','g',)

这可能吗?我该怎么做?

Is that possible? How can I do it?

推荐答案

除了 Douglas Leeder 的解决方案,解释如下:

在正则表达式中,组数是固定的.将量词放在组后面不会增加组数(想象所有其他组索引都会增加,因为一个更早的组匹配不止一次).

In regular expressions the group count is fixed. Placing a quantifier behind a group does not increase group count (imagine all other group indexes increment because an eralier group matched more than once).

当需要多次匹配时,带有量词的组是使复杂子表达式原子化的方法.正则表达式引擎只能将最后一个匹配项仅保存到组中.简而言之:没有办法用单一的非武装"正则表达式来实现你想要的,你必须找到另一种方式.

Groups with quantifiers are the way of making a complex sub-expression atomic, when there is need to match it more than once. The regex engine has no other way than saving the last match only to the group. In short: There is no way to achieve what you want with a single "unarmed" regular expression, and you have to find another way.

这篇关于Python 正则表达式 - 如何从通配符表达式中捕获多个组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆