110,502
社区成员
发帖
与我相关
我的任务
分享
Match m = Regex.Match("xbracadabra", "(a|b|r)");//(a|b|r)+
Console.WriteLine("Group:");
foreach (Group g in m.Groups) Console.Write("{0}:{1} ",g.Value,g.Index );
Regex r = new Regex("(Abc)+");
string str = "XYZAbcAbcAbcXYZAbcAb";
MatchCollection mc = r.Matches(str);
Console.WriteLine("---------------------Group-------MatchCollection={0}\n", mc.Count);
for (int i = 0; i < mc.Count; i++)
{
Match mt = mc[i];
Console.WriteLine("Match[{0}] = {1}", i, mt.Value);
for (int j = 0; j < mt.Groups.Count; j++)
{
Group gc = mt.Groups[j];
Console.WriteLine(" Group[{0}] value is:{1}. Index is:{2}", j, gc.Value, gc.Index);
for (int k = 0; k < gc.Captures.Count; k++)
{
Capture gg = gc.Captures[k];
Console.WriteLine(" Capture[{0}] value is:{1}. Index is:{2}", k, gg.Value, gg.Index);
}
}
Console.WriteLine("-------Next Match-----");
}
Console.WriteLine("\n---------------------Capture-------MatchCollection={0}\n", mc.Count);
for (int i = 0; i < mc.Count; i++)
{
Match mt = mc[i];
Console.WriteLine("Match[{0}] = {1}", i, mt.Value);
for (int j = 0; j < mt.Captures.Count; j++)
{
Capture gc = mt.Captures[j];
Console.WriteLine(" Capture[{0}] value is:{1}. Index is:{2}", j, gc.Value, gc.Index);
}
Console.WriteLine("-------Next Match-----");
}
Match m = r.Match(str);
GroupCollection gcc = m.Groups;
Console.WriteLine("\n\n--------------------------------GroupCollection={0}", gcc.Count);
for (int i = 0; i < gcc.Count; i++)
{
Group gt = gcc[i];
CaptureCollection cc = gt.Captures;
for (int j = 0; j < gt.Captures.Count; j++)
{
Capture ccnp = gt.Captures[j];
Console.WriteLine(" Capture[{0}] value is:{1}. Index is:{2}", j, ccnp.Value, ccnp.Index);
}
Console.WriteLine("-------Next Group-----");
}
CaptureCollection ccn = m.Captures;
Console.WriteLine("\n--------------------------------CaptureCollection={0}", ccn.Count);
for (int i = 0; i < ccn.Count; i++)
{
Capture gt = ccn[i];
Console.WriteLine(" Capture[{0}] value is:{1}. Index is:{2}", i, gt.Value, gt.Index);
Console.WriteLine("-------Next CaptureCollection-----");
}