MatchCollection Class
.NET Framework 3.0
Represents the set of successful matches found by iteratively applying a regular expression pattern to the input string.
Namespace: System.Text.RegularExpressions
Assembly: System (in system.dll)
Assembly: System (in system.dll)
The collection is immutable (read-only) and has no public constructor. The Matches method returns a MatchCollection object.
The following example code illustrates the use of the MatchCollection class to interrogate a set of Match instances.
#using <System.dll> using namespace System; using namespace System::Text::RegularExpressions; int main() { // Define a regular expression for repeated words. Regex^ rx = gcnew Regex( "\\b(?<word>\\w+)\\s+(\\k<word>)\\b",static_cast<RegexOptions>(RegexOptions::Compiled | RegexOptions::IgnoreCase) ); // Define a test string. String^ text = "The the quick brown fox fox jumped over the lazy dog dog."; // Find matches. MatchCollection^ matches = rx->Matches( text ); // Report the number of matches found. Console::WriteLine( "{0} matches found.", matches->Count ); // Report on each match. for each (Match^ match in matches) { String^ word = match->Groups["word"]->Value; int index = match->Index; Console::WriteLine("{0} repeated at position {1}", word, index); } }
import System.*;
import System.Text.RegularExpressions.*;
public class Test
{
public static void main(String[] args)
{
// Define a regular expression for repeated words.
Regex rx = new Regex("\\b(?<word>\\w+)\\s+(\\k<word>)\\b",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
// Define a test string.
String text = "The the quick brown fox fox jumped over the "
+ "lazy dog dog.";
// Find matches.
MatchCollection matches = rx.Matches(text);
// Report the number of matches found.
Console.WriteLine("{0} matches found.", (Int32)matches.get_Count());
// Report on each match.
for (int iCtr = 0; iCtr < matches.get_Count(); iCtr++) {
Match match = matches.get_Item(iCtr);
String word = match.get_Groups().get_Item("word").get_Value();
int index = match.get_Index();
Console.WriteLine("{0} repeated at position {1}", word,
(Int32)index);
}
} //main
} //Test
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show: