python 正则表达式:命名组中的重复名称

python regex: duplicate names in named groups

有没有办法在 python 的正则表达式命名组中使用相同的名称? 例如(?P<n>foo)|(?P<n>bar).

用例: 我正在尝试使用此正则表达式捕获 typeid
/(?=videos)((?P<type>videos)/(?P<id>\d+))|(?P<type>\w+)/?(?P<v>v)?/?(?P<id>\d+)?
来自这个字符串:

现在我收到错误: redefinition of group name 'id' as group 6; was group 3

答案是:Python re不支持同名组.

Python PyPi regex module 支持同名的命名捕获组:

The same name can be used by more than one group, with later captures ‘overwriting’ earlier captures. All of the captures of the group will be available from the captures method of the match object.

这是一个直播 Python 2.7 demo:

import regex
s = "foo bar"
rx = regex.compile(r"(?P<n>foo)|(?P<n>bar)")
print([x.group("n") for x in rx.finditer(s)])
// => ['foo', 'bar']

此外,在其他情况下,当您想要匹配多个备选方案并仅将部分捕获到一个组中时,您可以求助于分支重置功能:

Branch reset

(?|...|...)

Capture group numbers will be reused across the alternatives, but groups with different names will have different group numbers.

Examples:

>>> regex.match(r"(?|(first)|(second))", "first").groups()
('first',)
>>> regex.match(r"(?|(first)|(second))", "second").groups()
('second',)

Note that there is only one group.

你可以轻松变换

match(r'(?P<n>foo)|(?P<n>bar)', s)

进入

match(r'(?P<n>foo)', s) or match(r'(?P<n>bar)', s)