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

标签 c# .net regex text

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

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

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

和文本:

123|456|789|10|11

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

enter image description here

查看演示 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 按照它们在模式中出现的顺序分配组号,无论它们是否被命名。

关于c# - 正则表达式 - 为什么命名组位于组数组的末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29399436/

相关文章:

c# - 弹出菜单 windows phone 8.1 暂停 Listview

c# - HTML 电子邮件 "opens"未被跟踪

c# - LINQ 选择列值为 NULL 的行

c# - 如何将 XSD.exe 与 attributeGroup ref 一起使用

c# - Asp.net 中的母版页未加载代码实现

php - 忽略 preg_replace 的第二个实例

c# - IGrouping 的实现类是什么?

c# - 在 .NET 中运行线程操作时如何响应事件

javascript - 通过正则表达式拆分字符串

java - 正则表达式,省略几个单词