int counter;
            Match m;
            CaptureCollection cc;
            GroupCollection gc;
         
            Regex r = new Regex("(Abc)+");
            m = r.Match("XYZAbcAbcAbcXYZ");          
            gc = m.Groups;   
           
            Console.WriteLine("Captured groups = " + gc.Count.ToString());     
     
            for (int i = 0; i < gc.Count; i++)
            {
                //cc = gc[i].Captures;
                cc = m.Groups[i].Captures;
                counter = cc.Count;                             
                Console.WriteLine("Captures count = " + counter.ToString());              
                for (int ii = 0; ii < counter; ii++)
                {                                    
                    Console.WriteLine(cc[ii] + "   Starts at character " +cc[ii].Index);
                }
            }
            // The example displays the following output:
            //       Captured groups = 2
            //       Captures count = 1
            //       AbcAbcAbc   Starts at character 3
            //       Captures count = 3
            //       Abc   Starts at character 3
            //       Abc   Starts at character 6
            //       Abc   Starts at character 9  
哪位仁兄给我看看这段代码呗,我不明白为什么gc.Count.ToString()是2,还有是怎么迭代进行捕获的
 

解决方案 »

  1.   

    gc.Count //2整体的匹配结果Groups[0]算一个分组
    加上你加上的那个分组就变成2了..
    所以自定义的分组都是从Groups[1]开始取的
      

  2.   

     Match m;
                CaptureCollection cc;
                GroupCollection gc;            Regex r = new Regex("(Abc)+");
                m = r.Match("XYZAbcAbcAbcXYZ");
                gc = m.Groups;
                for (int i = 0; i < gc.Count; i++)
                {
                    Console.WriteLine("Captured groups"+i+" = " + gc[i]); 
                }
              
                Console.ReadKey();
      

  3.   

    http://webservices.ctocio.com.cn/net/270/9388270.shtml
      

  4.   

    有人能帮我就本例,具体点解释一下内部的组是怎么迭代的吗?
    还有capture和group的具体关系是怎么样的,谁继承谁的具体情况
      

  5.   

    你的代码里只有一个 Match,AbcAbcAbc,其中第一个 Group 是 AbcAbcAbc,第二个 Group 是 Abc,所以是 2
      

  6.   

    Match 是一个 Group,Group 是一个 Capture,反过来则不是,不是所有的 Capture 都放到 Groups 里面,如果模式改成,(?:Abc)+,则 Match 中只有一个 Group,AbcAbcAbc