Section 15.4. The Regex Class


15.4. The Regex Class

The .NET Framework provides an object-oriented approach to regular expression pattern matching and replacement.

The Framework Class Library namespace System.Text.RegularExpressions is the home to all the .NET Framework objects associated with regular expressions. The central class for regular expression support is Regex , which provides methods and properties for working with regular expressions, the most important of which are shown in Table 15-3.

Table 15-3. Regex members

Method or property

Explanation

Regex constructor

Overloaded; creates an instance of Regex

Options

Property that returns the options passed in to the constructor

IsMatch( )

Method that indicates whether a match is found in the input string

Match

Searches an input string and returns a match for a regular expression

Matches

Searches an input string and returns all successful matches for a regular expression

Replace

Replaces all occurrences of a pattern with a replacement string

Split

Splits an input string into an array of substrings based on a regular expression


Example 15-9 rewrites Example 15-8 to use regular expressions and thus solve the problem of searching for more than one type of delimiter .

Example 15-9. Regular expressions
 using System; using System.Text; using System.Text.RegularExpressions; namespace RegularExpressions {    class Tester    {       public void Run(  )       {          string s1 =          "One,Two,Three Liberty Associates, Inc.";          Regex theRegex = new Regex( " , ," );          StringBuilder sBuilder = new StringBuilder(  );          int id = 1;          foreach ( string subString in theRegex.Split( s1 ) )          {             sBuilder.AppendFormat(             "{0}: {1}\n", id++, subString );          }          Console.WriteLine( "{0}", sBuilder );       }       static void Main(  )       {          Tester t = new Tester(  );          t.Run(  );       }    } } 

The output looks like this:

 1: One     2: Two     3: Three     4: Liberty     5: Associates     6: Inc. 

Example 15-9 begins by creating a string, s1 , identical to the string used in Example 15-8:

 string s1 = "One,Two,Three Liberty Associates, Inc."; 

and a regular expression that is used to search the string:

 Regex theRegex = new Regex(" ,, "); 

One of the overloaded constructors for Regex takes a regular expression string as its parameter.

This can be a bit confusing. In the context of a C# program, which is the regular expressionthe text passed in to the constructor or the Regex object itself? It is true that the text string passed to the constructor is a regular expression in the traditional sense of the term . From a C# (that is, object-oriented) point of view, however, the argument to the constructor is just a string of characters ; it is the object called the Regex that is the regular expression object.


The rest of the program proceeds like Example 15-8, except that rather than calling the Split( ) method of String on string s1 , the Split( ) method of Regex is called. theRegex.Split( ) acts in much the same way as String.Split( ) , returning an array of strings as a result of matching the regular expression pattern within theRegex . Because it matches a regular expression, rather than using a set of delimiters, you have much greater control over how the string is split.



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net