正则表达式 - 为什么命名组位于组数组的末尾?

Regex - Why are the named groups located at the end of the group array?

为什么命名组位于组数组的末尾?

例如模式(具有命名和未命名组):

(?<digit>\d+)\|(?<main>\d+)\|(\d+)\|(?<abc>\d+)\|(\d+)

和文字:

123|456|789|10|11

为什么“789”和“11”位于数组的开头?

观看演示 ​​systemtextregularexpressions.com

对不起我的英语:)

嗯...这只是 .NET Framework 中的一个实现选择。

documentation算法说明:

Both unnamed and named capturing groups can be accessed by number. Unnamed groups are numbered from left to right starting with 1. (The capturing group in index 0 (zero) represents the match as a whole.) Named groups are then numbered from left to right starting with a number that is one greater than the number of unnamed capturing groups.

这不一定适用于其他风格,例如 PCRE 按照它们在模式中出现的顺序分配组号,无论它们是否被命名。