Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex.Match and noncapturing groups

Tags:

c#

.net

regex

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?

like image 464
elevener Avatar asked Aug 10 '11 06:08

elevener


People also ask

How do you write a non-capturing group in regex?

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)

What is the difference between a match and group in regex?

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).

What are capture groups in regex?

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" .

How do I match a group in regex?

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.


1 Answers

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

  1. Groups[1].Captures[0] is "a"
  2. Groups[1].Captures[1] is "b"
  3. Groups[1].Captures[2] is "c".
like image 107
Petar Ivanov Avatar answered Sep 28 '22 18:09

Petar Ivanov