为什么不超出范围索引对Regex组汇编的异常?
通常,如果您尝试访问不存在的数组成员,则会获得“ indexoutofrangeException”。
但是,由于某种原因,我没有为Regex集体汇编而理解。
例如:
var match = Regex.Match("", "a");
var test = match.Groups[6];
尽管匹配
只有1个组,但不要引发异常。
为什么会发生这种情况?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是设计。
参见 groupCollection 文档:
我对句子的开头进行了粗体,因为它描述了您当前的方案:
a
Regex模式在空字符串中没有匹配,该匹配是空的,并且没有ID = 6的组。This is by design.
See the
GroupCollection
documentation:I bolded the beginning of the sentence since it describes your current scenario:
a
regex pattern finds no matches in an empty string, the match is empty, and there is no group with ID = 6.