Can anyone explain why Regex.Match captures noncapturing groups. Can't find anything about it 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();
}
produces output
a
a
instead of empty one?
Sometimes, you may want to create a group but don't want to capture it in the groups of the match. To do that, you can use a non-capturing group with the following syntax: (?:X)
A Match is an object that indicates a particular regular expression matched (a portion of) the target text. A Group indicates a portion of a match, if the original regular expression contained group markers (basically a pattern in parentheses).
Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d" "o" and "g" .
Regular expressions allow us to not just match text but also to extract information for further processing. This is done by defining groups of characters and capturing them using the special parentheses ( and ) metacharacters. Any subpattern inside a pair of parentheses will be captured as a group.
Captures is different than groups.
matchResults.Groups[0]
is always the whole match. So your group would have been
matchResults.Groups[1],
if the regex were "(a)"
. Now since it's "(?:a)"
, you can check that it's empty.
Captures are a separate thing - they allow you to do something like this:
If you have the regex "(.)+"
, then it would match the string "abc"
.
Group[1] then would be "c", because that is the last group, while
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With