The use of regular expressions has been typically available to Unix shell scripts and Perl programmers. Of course, pattern matching has definitely found more uses beyond shell programming. For instance, heavy-duty text parsing is greatly simplified by using pattern-matching concepts. Whereas third-party libraries and implementations have been available for providing pattern-matching capabilities to mainstream developers, regular expression class libraries have never been "out of the box." .NET FCL changes the scenario with the inclusion of System.Text.RegularExpressions namespace. For example, Listing 4.12 shows the use of regular expressions to validate phone numbers . Regular expressions are particularly useful for validating user input. In fact, the ASP.NET Web application framework provides a prebuilt control for validating user input using a regular expression pattern. Listing 4.12 Using Regular Expressionsusing System; using System.Text.RegularExpressions; class RegExp { public static void Main() { String[] phone_nos = {"732-123-4567","7312-1243-4676"}; String phone_regexp = "[0-9]{3}-[0-9]{3}-[0-9]{4}"; foreach (String phone in phone_nos) { Match m = Regex.Match(phone,phone_regexp); if (m.Success) { Console.WriteLine(phone+" is Valid"); } else { Console.WriteLine(phone+" is Not Valid"); } } } } |