Python 带有组重复的正则表达式 groupDict

Python regex groupDict with repetitions of groups

想知道是否有像 match.groupdict() 这样的函数可以捕捉类似于 match.captures 函数的重复。

当我运行这段代码时:

import regex

test = regex.compile("(?P<a>a)*(?P<b>b)*(?P<c>c)*")
test.match("aabbbcc").groupdict()

我得到:

{'a': 'a', 'b': 'b', 'c': 'c'}

我想要的是这样的:

{'a': ['a', 'a'], 'b': ['b', 'b', 'b'], 'c': ['c', 'c']}

是否有这样做的功能,还是我应该自己手动完成?

你可以使用

import regex
test=regex.compile("(?P<a>a)*(?P<b>b)*(?P<c>c)*")
print ( test.match("aabbbcc").capturesdict() )
# => {'a': ['a', 'a'], 'b': ['b', 'b', 'b'], 'c': ['c', 'c']}

或者,如果您需要将整个字符串与此模式匹配,请将 .match 替换为 .fullmatch:

test.fullmatch("aabbbcc").capturesdict()

参见Python demo

参见 PyPi regex module documentation:

capturesdict returns a dict of the named groups and lists of all the captures of those groups.