String^ text = "One fish two fish red fish blue fish";
String^ pat = "(?<1>\\w+)\\s+(?<2>fish)\\s*";
// Compile the regular expression.
Regex^ r = gcnew Regex( pat,RegexOptions::IgnoreCase );
// Match the regular expression pattern against a text string.
Match^ m = r->Match(text);
while ( m->Success )
{
// Display the first match and its capture set.
Console::WriteLine( "Match=[{0}]", m );
CaptureCollection^ cc = m->Captures;
for each (Capture^ c in cc)
{
System::Console::WriteLine("Capture=[" + c + "]");
}
// Display Group1 and its capture set.
Group^ g1 = m->Groups[ 1 ];
Console::WriteLine( "Group1=[{0}]", g1 );
for each (Capture^ c1 in g1->Captures)
{
System::Console::WriteLine("Capture1=[" + c1 + "]");
}
// Display Group2 and its capture set.
Group^ g2 = m->Groups[ 2 ];
Console::WriteLine( "Group2=[{0}]", g2 );
for each (Capture^ c2 in g2->Captures)
{
System::Console::WriteLine("Capture2=[" + c2 + "]");
}
// Advance to the next match.
m = m->NextMatch();
}
// The example displays the following output:
// Match=[One fish ]
// Capture=[One fish ]
// Group1=[One]
// Capture1=[One]
// Group2=[fish]
// Capture2=[fish]
// Match=[two fish ]
// Capture=[two fish ]
// Group1=[two]
// Capture1=[two]
// Group2=[fish]
// Capture2=[fish]
// Match=[red fish ]
// Capture=[red fish ]
// Group1=[red]
// Capture1=[red]
// Group2=[fish]
// Capture2=[fish]
// Match=[blue fish]
// Capture=[blue fish]
// Group1=[blue]
// Capture1=[blue]
// Group2=[fish]
// Capture2=[fish]