[ Team LiB ] Previous Section Next Section

RegexCF 1.0, serializable

System.Text.RegularExpressions (system.dll)class

This class represents a regular expression. Use it to search for patterns in string data. It provides static methods that search for a pattern without explicitly creating Regex instances as well as instance methods that allow you to interact with a Regex object.

The various static methods employed by Regex take the input string to search for as the first argument and the regular expression pattern string as the second. This is equivalent to constructing a Regex instance with a pattern string, using it, and destroying it immediately. Most methods are overloaded as instance methods as well. These do not require a pattern argument, as this is provided with the constructor.

The Match( ) and Matches( ) methods search an input string for a single match or all matches. Their overloads are the same. The first argument is the input string. You can specify which position in the string the search should start at using a second integer parameter. Match( ) also lets you specify the length of substring to search after that position. IsMatch( ) works the same way as Match( ), except that it returns a boolean indicating whether the string contains a match.

The Split( ) method acts like the System.String.Split( ) method. It uses the Regex pattern as a delimiter to split the input string into an array of substrings. (The delimiter is not included in the substrings.) You can provide a maximum number of substrings to return, in which case the last substring returned is the remainder of the input string. You can also specify the position to start in the input string and a RegexOptions parameter.

The Replace( ) method uses a replacement string to replace each pattern match in an input string. The replacement string can include regular characters and backreference variable constructs (e.g., $1 or ${name}). Replace( ) can iterate through every match found in the input string, or it can specify a maximum number of replacements to perform. Replace( ) can also take an argument specifying a MatchEvaluator delegate, which is called every time a match is found.

Two additional static methods can transform strings used with regular expressions. Escape( ) converts a string containing regular expression metacharacters by replacing them with escaped equivalents (for example, ? would be changed to \?). The set of metacharacters converted is \, *, +, ?, |, {, [, (, ), ^, $, ., #, and any whitespace. The Unescape( ) method replaces escaped characters within a string with their unescaped equivalents. Use Escape( ) and Unescape( ) when you need to use one of these metacharacters as a literal in a regular expression.

A set of instance methods for Regex provides information on the capturing groups contained in the expression. GetGroupNames( ) and GetGroupNumbers( ) each return an array containing the names of all the capture groups or numbers of all capture groups, respectively. The GroupNameFromNumber( ) and GroupNumberFromName( ) methods return the corresponding name or number from the argument given to them.

CompileToAssembly( ) allows you to create your own type for a regular expression object and save it to disk as an assembly. This is a static method that takes a RegexCompilationInfo object and assembly information to build the type. The RegexCompilationInfo object contains the regular expression pattern and additional information needed for the compilation.

public class Regex : System.Runtime.Serialization.ISerializable {
// Public Constructors
   public Regex(string pattern);
   public Regex(string pattern, RegexOptions options);
// Protected Constructors
   protected Regex( );
// Public Instance Properties
   public RegexOptions Options{get; }
   public bool RightToLeft{get; }
// Public Static Methods
   public static void CompileToAssembly(RegexCompilationInfo[ ] regexinfos,
       System.Reflection.AssemblyName assemblyname);
   public static void CompileToAssembly(RegexCompilationInfo[ ] regexinfos,
       System.Reflection.AssemblyName assemblyname, 
       System.Reflection.Emit.CustomAttributeBuilder[ ] attributes);
   public static void CompileToAssembly(RegexCompilationInfo[ ] regexinfos,
       System.Reflection.AssemblyName assemblyname,
       System.Reflection.Emit.CustomAttributeBuilder[ ] attributes, string resourceFile);
   public static string Escape(string str);
   public static bool IsMatch(string input, string pattern);
   public static bool IsMatch(string input, string pattern, RegexOptions options);
   public static Match Match(string input, string pattern);
   public static Match Match(string input, string pattern, RegexOptions options);
   public static MatchCollection Matches(string input, string pattern);
   public static MatchCollection Matches(string input, string pattern, RegexOptions options);
   public static string Replace(string input, string pattern, MatchEvaluator evaluator);
   public static string Replace(string input, string pattern, MatchEvaluator evaluator,
        RegexOptions options);
   public static string Replace(string input, string pattern, string replacement);
   public static string Replace(string input, string pattern, string replacement, 
        RegexOptions options);
   public static string[ ] Split(string input, string pattern);
   public static string[ ] Split(string input, string pattern, RegexOptions options);
   public static string Unescape(string str);
// Public Instance Methods
   public string[ ] GetGroupNames( );
   public int[ ] GetGroupNumbers( );
   public string GroupNameFromNumber(int i);
   public int GroupNumberFromName(string name);
   public bool IsMatch(string input);
   public bool IsMatch(string input, int startat);
   public Match Match(string input);
   public Match Match(string input, int startat);
   public Match Match(string input, int beginning, int length);
   public MatchCollection Matches(string input);
   public MatchCollection Matches(string input, int startat);
   public string Replace(string input, MatchEvaluator evaluator);
   public string Replace(string input, MatchEvaluator evaluator, int count);
   public string Replace(string input, MatchEvaluator evaluator, int count, int startat);
   public string Replace(string input, string replacement);
   public string Replace(string input, string replacement, int count);
   public string Replace(string input, string replacement, int count, int startat);
   public string[ ] Split(string input);
   public string[ ] Split(string input, int count);
   public string[ ] Split(string input, int count, int startat);
   public override string ToString( );    
// overrides object
// Protected Instance Methods
   protected override void Finalize( );   
// overrides object
   protected void InitializeReferences( );
   protected bool UseOptionC( );
   protected bool UseOptionR( );
}

Passed To

System.Net.WebPermission.{AddPermission( ), WebPermission( )}

    [ Team LiB ] Previous Section Next Section