Mega Code Archive

 
Categories / C# Tutorial / Regular Expression
 

Using Match Collection

using System; using System.Text.RegularExpressions;     class Test     {         public static void Main()         {             string string1 = "This is a test string";             Regex theReg = new Regex(@"(\S+)\s");             MatchCollection theMatches = theReg.Matches(string1);             foreach (Match theMatch in theMatches)             {                 Console.WriteLine(theMatch.Length);                 if (theMatch.Length != 0)                 {                     Console.WriteLine("theMatch: {0}",                                   theMatch.ToString());                 }             }         }     }