具有多个匹配项的正则表达式组捕获 [英] Regular expression group capture with multiple matches
问题描述
快速正则表达式问题.
我试图在 python 中捕获一个捕获组的多个实例(不要认为它是特定于 python 的),但随后的捕获似乎覆盖了以前的.
Quick regular expression question.
I'm trying to capture multiple instances of a capture group in python (don't think it's python specific), but the subsequent captures seems to overwrite the previous.
在这个过于简化的例子中,我实际上是在尝试拆分一个字符串:
In this over-simplified example, I'm essentially trying to split a string:
x = 'abcdef'
r = re.compile('(\w){6}')
m = r.match(x)
m.groups() # = ('f',) ?!?
我想得到 ('a', 'b', 'c', 'd', 'e', 'f')
,但是因为正则表达式会覆盖后续的捕获,所以我得到 ('f',)
I want to get ('a', 'b', 'c', 'd', 'e', 'f')
, but because regex overwrites subsequent captures, I get ('f',)
这是正则表达式应该表现的方式吗?有没有办法做我想做的事而不必重复六次语法?
Is this how regex is supposed to behave? Is there a way to do what I want without having to repeat the syntax six times?
提前致谢!
安德鲁
Thanks in advance!
Andrew
推荐答案
恐怕您不能为此使用群组.每个组只能匹配一次,我相信所有正则表达式都是这样工作的.一个可能的解决方案是尝试使用 findall() 或类似方法.
You can't use groups for this, I'm afraid. Each group can match only once, I believe all regexes work this way. A possible solution is to try to use findall() or similar.
r=re.compile(r'\w')
r.findall(x)
# 'a', 'b', 'c', 'd', 'e', 'f'
这篇关于具有多个匹配项的正则表达式组捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!