ex-10-07 MatchCollection and Match 1004 Bytes
Newer Older
O'Reilly Media, Inc.'s avatar
O'Reilly Media, Inc. committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// Example 10-07: Using MatchCollection and Match

namespace Programming_CSharp
{
   using System;
   using System.Text.RegularExpressions;

   class Test
   {
      public static void Main()
      {
         string string1 = "This is a test string";
            
         // find any nonwhitespace followed by whitespace
         Regex theReg = new Regex(@"(\S+)\s");
        
         // get the collection of matches
         MatchCollection theMatches = 
            theReg.Matches(string1);

         // iterate through the collection
         foreach (Match theMatch in theMatches)
         {
            Console.WriteLine(
               "theMatch.Length: {0}", theMatch.Length);

            if (theMatch.Length != 0)               
            {
               Console.WriteLine("theMatch: {0}", 
                  theMatch.ToString());              
            }       
         }           
      }               
   }                   
}