using System;
using System.Text.RegularExpressions;
class MainClass
{
public static void Main()
{
string text = "(800) 888-1211\n" +
"(212) 555-1212\n" +
"(506) 777-1213\n" +
"(650) 222-1214\n" +
"(888) 111-1215\n";
string areaCodeRegExp = @"(?<areaCodeGroup>\(\d\d\d\))";
string phoneRegExp = @"(?<phoneGroup>\d\d\d\-\d\d\d\d)";
MatchCollection myMatchCollection = Regex.Matches(text, areaCodeRegExp + " " + phoneRegExp);
foreach (Match myMatch in myMatchCollection)
{
Console.WriteLine("Area code = " + myMatch.Groups["areaCodeGroup"]);
Console.WriteLine("Phone = " + myMatch.Groups["phoneGroup"]);
foreach (Group myGroup in myMatch.Groups)
foreach (Capture myCapture in myGroup.Captures)
Console.WriteLine("myCapture.Value = " + myCapture.Value);
}
}
}
|