C# Regular expression returns group multiple times

前端 未结 1 824
予麋鹿
予麋鹿 2021-01-23 15:42

I have a very simple regex like this in C#:

(var \\= 0\\;)

But when I try to match this against a string that has only one occurrence of the pa

相关标签:
1条回答
  • 2021-01-23 16:19

    According to the documentation, the first element of the GroupCollection is the entire match, not the first group created by ().

    From near the bottom of the Remarks section here:

    If the regular expression engine can find a match, the first element of the GroupCollection object returned by the Groups property contains a string that matches the entire regular expression pattern. Each subsequent element > represents a captured group, if the regular expression includes capturing groups.

    Due to this, both items 0 and 1 are identical given the RegEx you are currently using. To only see the actual group matches, you could skip the first element of the GroupCollection, and only process the groups you have defined in the RegEx.

    EDIT

    After investigating the additional data, I think I may have found the cause of your duplicates.

    I believe that you are seeing more than one Match, and so the outer foreach loop runs twice, not once. This is because there are 2 separate lines with "= 0;" in the example.

    Here is LinqPad example code that shows 2 matches being found, and therefore multiple duplicate groups being output. (note, I used the simple regex you provided to test, since the long regex didn't provide any matches)

    static string inputStr = "# preceding comment \r\n" + 
    "class\r\n" + 
    "{\r\n" + 
    "   (param1 = \"val1\", param2 = \"val2\", param3 = val3)\r\n" + 
    "}\r\n" + 
    "[\r\n" + 
    "    # inside comment\r\n" + 
    "    setting1 = 0;\r\n" + 
    "    setting2 = 0;\r\n" + 
    "]\r\n";
    
    const string REGEX = "(\\= [0]\\;)";
    
    void Main()
    {
    
        var regex = new System.Text.RegularExpressions.Regex(REGEX);
        MatchCollection matches = regex.Matches(inputStr);
        Console.WriteLine("Matches:{0}", matches.Count);
        int matchCnt = 0;
        foreach (Match m in matches)
        {
            int groupCnt = 0;
            foreach (Group g in m.Groups)
            {
                Console.WriteLine("match[{0}] group[{1}]: Captures:{2} '{3}'", matchCnt, groupCnt, g.Captures.Count, g);
                //g.Dump();
                groupCnt++;
            }
            matchCnt++;
        }
        Console.WriteLine("Done!");
    }
    

    And here is the output generated by LinqPad when this code runs:

    Matches:2
    match[0] group[0]: Captures:1 '= 0;'
    match[0] group[1]: Captures:1 '= 0;'
    match[1] group[0]: Captures:1 '= 0;'
    match[1] group[1]: Captures:1 '= 0;'
    Done!
    
    0 讨论(0)
提交回复
热议问题