Regex.Match and nonaptapturing groups

Can anyone explain why Regex.Match captures exciting bands. Cannot find anything in MSDN. Why

Regex regexObj = new Regex("(?:a)"); Match matchResults = regexObj.Match("aa"); while (matchResults.Success) { foreach (Capture g in matchResults.Captures) { Console.WriteLine(g.Value); } matchResults = matchResults.NextMatch(); } 

outputs a conclusion

 a a 

instead of empty?

+6
source share
1 answer

Captures are different from groups.

 matchResults.Groups[0] 

always coincides with everything. So your group would be

 matchResults.Groups[1], 

if the regular expression was "(a)" . Now, since it is "(?:a)" , you can verify that it is empty.

Captures are a separate thing - they allow you to do something like this:

If you have the regular expression "(.)+" , Then it will match the string "abc" .

Group [1] will then be “c,” because this is the last group, and

  • Groups [1]. Captures [0] is "a"
  • Groups [1]. Grips [1] - "b"
  • Groups [1]. Captures [2] is the "c".
+7
source

Source: https://habr.com/ru/post/894694/


All Articles