MatchCollection クラス
アセンブリ: System (system.dll 内)



MatchCollection クラスを使用し、Match インスタンスのセットを問い合わせるコード例を次に示します。
using System; using System.Text.RegularExpressions; public class Test { public static void Main () { // 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.", matches.Count); // Report on each match. foreach (Match match in matches) { string word = match.Groups["word"].Value; int index = match.Index; Console.WriteLine("{0} repeated at position {1}", word, index); } } }
#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

System.Text.RegularExpressions.MatchCollection


Windows 98, Windows 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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- MatchCollection クラスのページへのリンク