Mega Code Archive

 
Categories / C# / Development Class
 

Gets a collection of groups matched by the regular expression

using System; using System.Text.RegularExpressions; public class Example {    public static void Main()    {       string pattern = @"(\d{3})-(\d{3}-\d{4})";       string input = "111-222-1111 222-111-1111 333-222-3333 444-888-9999";       MatchCollection matches = Regex.Matches(input, pattern);       foreach (Match match in matches)       {          Console.WriteLine("Area Code:        {0}", match.Groups[1].Value);          Console.WriteLine("Telephone number: {0}", match.Groups[2].Value);          Console.WriteLine();       }    } }