在C#中的正则表达式为什么最初的比赛出现在群体? [英] In C# regular expression why does the initial match show up in the groups?

查看:93
本文介绍了在C#中的正则表达式为什么最初的比赛出现在群体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,如果我写的正则表达式这是比赛,我可以拿到赛或者我可以访问它的群体。这似乎计数器直观,因为这些组中与括号中的表达定义(和)。现在看来似乎不仅是错误的,但多余的。任何一个知道为什么

 正则表达式快速检查=新的正则表达式(@(\D +)\d +)?; 
串源=ABC123

m.Value //等于源
m.Groups.Count //等于2
m.Groups [0])//等于源
m.Groups [1])//等于ABC


解决方案

我同意 - 这是一个有些奇怪,但是我觉得有好的原因吧。



一个正则表达式匹配是本身就是一个,这又是一个拍摄



Match.Value (或 Capture.Value ,因为它实际上是)是唯一有效的,当一个匹配目前在字符串中 - 如果你匹配一个模式的多个实例,然后通过定义,它不能返回的一切。实际上 - 在比赛中的属性是当只有匹配便利



但澄清。其中,通过整场比赛进组[0] 有道理的这种行为 - 考虑一个天真的代码unminifier这一(人为)例如:

  [TestMethod的] 
公共无效UnMinifyExample()
{
串toUnMinify ={INT someValue中= 0; / *初始化的值* /} / * * /结束;
字符串结果= Regex.Replace(toUnMinify,@(; |})\s *(/ \ * [^ *] * \ * /)\s *?,$ 0\\ \
);
Assert.AreEqual({INT someValue中= 0; / *初始化值* / \\\
} / * * /结束\\\
,结果);
}



正则表达式匹配将在声明的结尾保存/ * * /注释放置一个换行符之后 - 但是适用于两种; 。或}行结束



好了 - 你可能想知道为什么你会不屑于正则表达式这样做 - 但我的幽默:)



如果组[0] 由匹配生成此正则表达式不是全部捕获 - 然后单调用替换是不可能的 - 和你的问题很可能会问为什么的的整场比赛得到投入组[0] ,而不是倒过来!


So if I write a regex it's matches I can get the match or I can access its groups. This seems counter intuitive since the groups are defined in the expression with braces "(" and ")". It seems like it is not only wrong but redundant. Any one know why?

Regex quickCheck = new Regex(@"(\D+)\d+");
string source = "abc123";

m.Value        //Equals source
m.Groups.Count //Equals 2
m.Groups[0])   //Equals source
m.Groups[1])   //Equals "abc"

解决方案

I agree - it is a little strange, however I think there are good reasons for it.

A Regex Match is itself a Group, which in turn is a Capture.

But the Match.Value (or Capture.Value as it actually is) is only valid when one match is present in the string - if you're matching multiple instances of a pattern, then by definition it can't return everything. In effect - the Value property on the Match is a convenience for when there is only match.

But to clarify where this behaviour of passing the whole match into Groups[0] makes sense - consider this (contrived) example of a naive code unminifier:

[TestMethod]
public void UnMinifyExample()
{
  string toUnMinify = "{int somevalue = 0; /*init the value*/} /* end */";
  string result = Regex.Replace(toUnMinify, @"(;|})\s*(/\*[^*]*?\*/)?\s*", "$0\n");
  Assert.AreEqual("{int somevalue = 0; /*init the value*/\n} /* end */\n", result);
}

The regex match will preserve /* */ comments at the end of a statement, placing a newline afterwards - but works for either ; or } line-endings.

Okay - you might wonder why you'd bother doing this with a regex - but humour me :)

If Groups[0] generated by the matches for this regex was not the whole capture - then a single-call replace would not be possible - and your question would probably be asking why doesn't the whole match get put into Groups[0] instead of the other way round!

这篇关于在C#中的正则表达式为什么最初的比赛出现在群体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆