System.Text.RegularExpressions.Regex Class

Assembly: System.dll
Namespace: System.Text.RegularExpressions
Summary
Represents an immutable regular expression.
C# Syntax:
[Serializable]
public class Regex : ISerializable
Remarks
The Regex class contains several static methods that allow you to use a regular expression without explicitly creating a Regex object. Using a static method is equivalent to constructing a Regex object, using it once and then destroying it.
See also:
System.Text.RegularExpressions Namespace See also:
MSDN: comregularexpressions | MSDN: regularexpressionslanguageelements

System.Text.RegularExpressions.Regex Member List:

Public Constructors
ctor #2 Overloaded:
.ctor(string pattern)

Initializes and compiles an instance of the Regex class for the specified regular expression.
ctor #3 Overloaded:
.ctor(string pattern, RegexOptions options)

Initializes and compiles an instance of the Regex class for the specified regular expression, with options that modify the pattern.
Public Properties
Options Read-only

Returns the options passed into the Regex constructor.
RightToLeft Read-only

Gets a value indicating whether the regular expression searches from right to left.
Public Methods
CompileToAssembly Overloaded:
CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname)

Compiles one or more specified Regex objects to a named file.
CompileToAssembly Overloaded:
CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[] attributes)

Compiles one or more specified Regex objects to a named file with specified attributes.
CompileToAssembly Overloaded:
CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[] attributes, string resourceFile)

Compiles one or more specified Regex objects and a specified resource file to a named assembly with specified attributes.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
Escape Escapes a minimal set of metacharacters (\, *, +, ?, |, {, [, (, ), ^, $, ., #, and white space) by replacing them with their escape codes.
GetGroupNames Returns an array of capturing group names for the regular expression.
GetGroupNumbers Returns an array of capturing group numbers that correspond to group names in an array.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
GroupNameFromNumber Gets the group name that corresponds to the specified group number.
GroupNumberFromName Returns the group number that corresponds to the specified group name.
IsMatch Overloaded:
IsMatch(string input)

Indicates whether the regular expression specified in the Regex constructor finds a match in the input string.
IsMatch Overloaded:
IsMatch(string input, int startat)

Indicates whether the regular expression specified in the Regex constructor finds a match in the input string beginning at the specified starting position in the string.
IsMatch Overloaded:
IsMatch(string input, string pattern)

Indicates whether the regular expression finds a match in the input string using the regular expression specified in the pattern parameter.
IsMatch Overloaded:
IsMatch(string input, string pattern, RegexOptions options)

Indicates whether the regular expression finds a match in the input string, using the regular expression specified in the pattern parameter and the matching options supplied in the options parameter.
Match Overloaded:
Match Match(string input)

Searches the specified input string for an occurrence of the regular expression specified in the Regex constructor.
Match Overloaded:
Match Match(string input, int startat)

Searches the input string for an occurrence of a regular expression with a specified input string starting position.
Match Overloaded:
Match Match(string input, string pattern)

Searches the specified input string for an occurrence of the regular expression supplied in the pattern parameter.
Match Overloaded:
Match Match(string input, int beginning, int length)

Searches the input string for an occurrence of a regular expression with a specified input string starting position and input string length.
Match Overloaded:
Match Match(string input, string pattern, RegexOptions options)

Searches the input string for an occurrence of the regular expression supplied in a pattern parameter with matching options supplied in an options parameter.
Matches Overloaded:
Matches(string input)

Searches the specified input string for all occurrences of a regular expression.
Matches Overloaded:
Matches(string input, int startat)

Searches the specified input string for all occurrences of a regular expression, beginning at the specified starting position in the string.
Matches Overloaded:
Matches(string input, string pattern)

Searches the specified input string for all occurrences of the regular expression specified in the pattern parameter.
Matches Overloaded:
Matches(string input, string pattern, RegexOptions options)

Searches the specified input string for all occurrences of the regular expression supplied in a pattern parameter with matching options supplied in an options parameter.
Replace Overloaded:
Replace(string input, MatchEvaluator evaluator)

Replaces all occurrences of a character pattern defined by the regular expression specified in the Regex constructor . A MatchEvaluator delegate is called at each match to evaluate the replacement.
Replace Overloaded:
Replace(string input, string replacement)

Replaces all occurrences of a specified regular expression pattern with a replacement string, starting at the first character in the input string.
Replace Overloaded:
Replace(string input, MatchEvaluator evaluator, int count)

Replaces up to a specified number of occurrences of a pattern defined by the regular expression specified in the Regex constructor with a replacement string, starting at the first character in the input string. A MatchEvaluator delegate is called at each match to evaluate the replacement.
Replace Overloaded:
Replace(string input, string replacement, int count)

Replaces up to a specified number of occurrences of a pattern defined by the regular expression specified in the Regex constructor with a specified replacement string, starting at the first character in the input string.
Replace Overloaded:
Replace(string input, string pattern, MatchEvaluator evaluator)

Replaces all occurrences of a character pattern defined by a regular expression with a replacement character string starting at the first character . A MatchEvaluator delegate is called at each match to evaluate the replacement.
Replace Overloaded:
Replace(string input, string pattern, string replacement)

Replaces all occurrences of matches defined by the regular expression with a replacement string, starting at the first character in the input string.
Replace Overloaded:
Replace(string input, MatchEvaluator evaluator, int count, int startat)

Replaces up to a specified number of occurrences of a pattern specified in the Regex constructor with a replacement string, starting at a specified character position in the input string. A MatchEvaluator delegate is called at each match to evaluate the replacement.
Replace Overloaded:
Replace(string input, string replacement, int count, int startat)

Replaces up to a specified number of occurrences of a pattern in the input string defined by the regular expression specified in the Regex constructor with a specified replacement string, starting at a specified character position in the input string.
Replace Overloaded:
Replace(string input, string pattern, MatchEvaluator evaluator, RegexOptions options)

Replaces all occurrences of a character pattern defined by a specified regular expression with a replacement character string starting at the first character . Options can be specified to modify matching behavior and a MatchEvaluator delegate is called at each match to evaluate the replacement.
Replace Overloaded:
Replace(string input, string pattern, string replacement, RegexOptions options)

Replaces all occurrences of a pattern defined by a specified regular expression with a specified replacement character string, starting at the first character in the input string. Options can be specified to modify matching behavior.
Split Overloaded:
Split(string input)

Splits the specified input string at the positions defined by a regular expression pattern specified in the Regex constructor..
Split Overloaded:
Split(string input, int count)

Splits the specified input string a specified maximum number of times at the positions defined by a regular expression specified in the Regex constructor.
Split Overloaded:
Split(string input, string pattern)

Splits the input string at the positions defined by a regular expression pattern.
Split Overloaded:
Split(string input, int count, int startat)

Splits the specified input string a specified maximum number of times at the positions defined by a regular expression specified in the Regex constructor, starting at a specified character position in the input string
Split Overloaded:
Split(string input, string pattern, RegexOptions options)

Splits the input string at the positions defined by a specified regular expression pattern. Options can be specified to modify matching behavior.
ToString Overridden:
Returns the regular expression pattern that was passed into the Regex constructor.
Unescape Unescapes any escaped characters in the input string.
Protected Constructors
ctor #1 Overloaded:
.ctor()

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the Regex class with no parameters.
Protected Methods
Finalize Overridden:
Forces a Regex object to free resources before the object is destroyed by the Garbage Collector.
InitializeReferences
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.
UseOptionC
UseOptionR

Hierarchy:


System.Text.RegularExpressions.Regex Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the Regex class with no parameters.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
protected Regex();

Return to top


Overloaded ctor #2
Summary
Initializes and compiles an instance of the Regex class for the specified regular expression.
C# Syntax:
public Regex(
   string pattern
);
Parameters:

pattern

The regular expression pattern to match.

Exceptions
Exception Type Condition
ArgumentException Regular expression parsing error
Remarks
A Regex object is immutable, which means that it can be used only for the match parameters defined when it is created. It can be used any number of times without being recompiled, however.

Return to top


Overloaded ctor #3
Summary
Initializes and compiles an instance of the Regex class for the specified regular expression, with options that modify the pattern.
C# Syntax:
public Regex(string pattern, Regex(
   string pattern,
   RegexOptions options
);
Parameters:

pattern

The regular expression pattern to match.

options

A bitwise OR combination of RegexOption enumeration values.

Exceptions
Exception Type Condition
ArgumentException Regular expression parsing error.
Remarks
A Regex object is immutable, which means that it can be used only for the match parameters defined when it is created. It can be used any number of times without being recompiled, however.

Return to top


Property: Options (read-only)
Summary
Returns the options passed into the Regex constructor.
C# Syntax:
public RegexOptions Options {get;}

Return to top


Property: RightToLeft (read-only)
Summary
Gets a value indicating whether the regular expression searches from right to left.
C# Syntax:
public bool RightToLeft {get;}
Remarks
RightToLeft is true if the Regex instance was constructed with the RegexOptions.RightToLeft option.

Return to top


Overloaded Method: CompileToAssembly(
   RegexCompilationInfo[] regexinfos,
   AssemblyName assemblyname
)
Summary
Compiles one or more specified Regex objects to a named file.
C# Syntax:
public static void CompileToAssembly(
   RegexCompilationInfo[] regexinfos,
   AssemblyName assemblyname
);
Parameters:

regexinfos

An array of RegexCompilationInfo objects describing the regular expressions to compile.

assemblyname

The file name of the assembly.

Exceptions
Exception Type Condition
ArgumentNullException assemblyname or regexinfos is null.

Return to top


Overloaded Method: CompileToAssembly(
   RegexCompilationInfo[] regexinfos,
   AssemblyName assemblyname,
   CustomAttributeBuilder[] attributes
)
Summary
Compiles one or more specified Regex objects to a named file with specified attributes.
C# Syntax:
public static void CompileToAssembly(
   RegexCompilationInfo[] regexinfos,
   AssemblyName assemblyname,
   CustomAttributeBuilder[] attributes
);
Parameters:

regexinfos

An array of RegexCompilationInfo objects describing the regular expressions to compile.

assemblyname

The file name of the assembly.

attributes

An array of CustomAttributeBuilder objects describing the attributes of the assembly.

Exceptions
Exception Type Condition
ArgumentNullException assemblyname or regexinfos is null.

Return to top


Overloaded Method: CompileToAssembly(
   RegexCompilationInfo[] regexinfos,
   AssemblyName assemblyname,
   CustomAttributeBuilder[] attributes,
   string resourceFile
)
Summary
Compiles one or more specified Regex objects and a specified resource file to a named assembly with specified attributes.
C# Syntax:
public static void CompileToAssembly(
   RegexCompilationInfo[] regexinfos,
   AssemblyName assemblyname,
   CustomAttributeBuilder[] attributes,
   string resourceFile
);
Parameters:

regexinfos

An array of RegexCompilationInfo objects describing the regular expressions to compile.

assemblyname

The file name of the assembly.

attributes

An array of CustomAttributeBuilder objects describing the attributes of the assembly.

resourceFile

The name of the Win32 resource file to include in the assembly.

Exceptions
Exception Type Condition
ArgumentNullException assemblyname or regexinfos is null.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Escape(
   string str
)
Summary
Escapes a minimal set of metacharacters (\, *, +, ?, |, {, [, (, ), ^, $, ., #, and white space) by replacing them with their escape codes.
C# Syntax:
public static string Escape(
   string str
);
Parameters:

str

The input string containing the text to convert.

Return Value:
A string of characters with any metacharacters converted to their escaped form.
Remarks
Escape converts a string so it can be used safely as a constant within a regular expression. (The number sign (#) and white-space characters must be escaped so the string can be used safely within an expression parsed with the RegexOptions.IgnorePatternWhitespace option enabled. If future .NET regular expression features add additional metacharacters, Escape converts those characters to escaped form as well.)

Return to top


Overridden Method: Finalize()
Summary
Forces a Regex object to free resources before the object is destroyed by the Garbage Collector.
C# Syntax:
~Regex();

Return to top


Method: GetGroupNames()
Summary
Returns an array of capturing group names for the regular expression.
C# Syntax:
public string[] GetGroupNames();
Return Value:
A string array of group names.
Remarks
The collection of group names contains the set of strings used to name capturing groups in the expression. Even if capturing groups are not explicitly named, they are automatically assigned numerical names (1, 2, 3, and so on). Therefore, this collection can be used to determine the number of groups.

Return to top


Method: GetGroupNumbers()
Summary
Returns an array of capturing group numbers that correspond to group names in an array.
C# Syntax:
public int[] GetGroupNumbers();
Return Value:
An integer array of group numbers.
Remarks
Referencing a group by its number instead of by string name can provide faster access.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GroupNameFromNumber(
   int i
)
Summary
Gets the group name that corresponds to the specified group number.
C# Syntax:
public string GroupNameFromNumber(
   int i
);
Parameters:

i

The group number to convert to the corresponding group name.

Return Value:
The string containing the group name associated with the specified group number.

Return to top


Method: GroupNumberFromName(
   string name
)
Summary
Returns the group number that corresponds to the specified group name.
C# Syntax:
public int GroupNumberFromName(
   string name
);
Parameters:

name

The group name to convert to the corresponding group number.

Return Value:
The group number that corresponds to the specified group name.

Return to top


Method: InitializeReferences()
C# Syntax:
protected void InitializeReferences();
Exceptions
Exception Type Condition
NotSupportedException References have already been initialized.

Return to top


Overloaded Method: IsMatch(
   string input
)
Summary
Indicates whether the regular expression specified in the Regex constructor finds a match in the input string.
C# Syntax:
public bool IsMatch(
   string input
);
Parameters:

input

The string to search for a match.

Return Value:
true if the regular expression finds a match; otherwise, false.

Return to top


Overloaded Method: IsMatch(
   string input,
   int startat
)
Summary
Indicates whether the regular expression specified in the Regex constructor finds a match in the input string beginning at the specified starting position in the string.
C# Syntax:
public bool IsMatch(
   string input,
   int startat
);
Parameters:

input

The string to search for a match.

startat

The character position at which to start the search.

Return Value:
true if the regular expression finds a match; otherwise, false.

Return to top


Overloaded Method: IsMatch(
   string input,
   string pattern
)
Summary
Indicates whether the regular expression finds a match in the input string using the regular expression specified in the pattern parameter.
C# Syntax:
public static bool IsMatch(
   string input,
   string pattern
);
Parameters:

input

The string to search for a match.

pattern

The regular expression pattern to match.

Return Value:
true if the regular expression finds a match; otherwise, false.
Remarks
The two static IsMatch methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method IsMatch. The static methods are provided to allow an isolated, single use of a regular expression without explicitly creating a Regex object.

Return to top


Overloaded Method: IsMatch(
   string input,
   string pattern,
   RegexOptions options
)
Summary
Indicates whether the regular expression finds a match in the input string, using the regular expression specified in the pattern parameter and the matching options supplied in the options parameter.
C# Syntax:
public static bool IsMatch(
   string input,
   string pattern,
   RegexOptions options
);
Parameters:

input

The string to search for a match.

pattern

The regular expression pattern to match.

options

A bitwise OR combination of RegexOption enumeration values.

Return Value:
true if the regular expression finds a match; otherwise, false.
Remarks
The two static IsMatch methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method IsMatch. The static methods are provided to allow an isolated, single use of a regular expression without explicitly creating a Regex object.

Return to top


Overloaded Method: Match(
   string input
)
Summary
Searches the specified input string for an occurrence of the regular expression specified in the Regex constructor.
C# Syntax:
public Match Match(
   string input
);
Parameters:

input

The string to search for a match.

Return Value:
A regular expression Match object.

Return to top


Overloaded Method: Match(
   string input,
   int startat
)
Summary
Searches the input string for an occurrence of a regular expression with a specified input string starting position.
C# Syntax:
public Match Match(
   string input,
   int startat
);
Parameters:

input

The string to search for a match.

startat

The character position at which to start the search.

Return Value:
A Regular Expression Match object.
Remarks
You can optionally specify a starting position in the string using the startat parameter. If you do not specify a starting position, the search begins at the default startat position. If the regular expression searches from left to right, the default startat position is at the beginning of the interval; if it searches from right to left, the default startat position is at the end of the inteval.

If you want to restrict a match so it does not scan, anchor the regular expression with a \G (at the left for a left-to-right pattern, or at the right for a right-to-left pattern). This restricts the match so it must start exactly at startpos.

When a Regex is left-to-right (the default), the match and the scan proceed rightward, starting at the character specified in startat. When a Regex is right-to-left (when it is constructed with the RegexOptions.RightToLeft option), the match and scan proceed in the opposite direction and begin with the character at startat -1.

Return to top


Overloaded Method: Match(
   string input,
   string pattern
)
Summary
Searches the specified input string for an occurrence of the regular expression supplied in the pattern parameter.
C# Syntax:
public static Match Match(
   string input,
   string pattern
);
Parameters:

input

The string to search for a match.

pattern

The regular expression pattern to match.

Return Value:
A regular expression Match object.
Remarks
The static Match methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Match. The static methods are provided to allow an isolated, single use of a regular expression without explicitly creating a Regex object.

Return to top


Overloaded Method: Match(
   string input,
   int beginning,
   int length
)
Summary
Searches the input string for an occurrence of a regular expression with a specified input string starting position and input string length.
C# Syntax:
public Match Match(
   string input,
   int beginning,
   int length
);
Parameters:

input

The string to be tested for a match.

beginning

The character position in the input string at which to begin the search.

length

The number of characters in the substring to include in the search.

Return Value:
A regular expression Match object.

Return to top


Overloaded Method: Match(
   string input,
   string pattern,
   RegexOptions options
)
Summary
Searches the input string for an occurrence of the regular expression supplied in a pattern parameter with matching options supplied in an options parameter.
C# Syntax:
public static Match Match(
   string input,
   string pattern,
   RegexOptions options
);
Parameters:

input

The string to be tested for a match.

pattern

The regular expression pattern to match.

options

A bitwise OR combination of RegexOption enumeration values.

Return Value:
A regular expression Match object.
Remarks
If you want to restrict a match so it does not scan, anchor the regular expression with a \G (at the left for a left-to-right pattern, or at the right for a right-to-left pattern). This restricts the match so it must start exactly at startpos.

When a Regex is left-to-right (the default), the match and the scan proceed rightward, starting at the character at startat. When a Regex is right-to-left (constructed with the RegexOptions.RightToLeft option enabled), the match and scan are in the opposite direction and begin with the character at startat -1.

The static Match methods are equivalent to constructing a Regex object with the specified pattern and calling the instance method Match. The static methods are provided to allow an isolated, single use of a regular expression without explicitly creating a Regex object.

Return to top


Overloaded Method: Matches(
   string input
)
Summary
Searches the specified input string for all occurrences of a regular expression.
C# Syntax:
public MatchCollection Matches(
   string input
);
Parameters:

input

The string to search for a match.

Return Value:
A MatchCollection of the Match objects found by the search.
Remarks
The Matches methods are similar to the Match methods, except they return the list of successful matches that would result from iteratively calling match= Match (...), then Match.NextMatch (), and so on. The collection includes only successful matches and terminates at the first unsuccessful match.

Return to top


Overloaded Method: Matches(
   string input,
   int startat
)
Summary
Searches the specified input string for all occurrences of a regular expression, beginning at the specified starting position in the string.
C# Syntax:
public MatchCollection Matches(
   string input,
   int startat
);
Parameters:

input

The string to search for a match.

startat

The character position in the input string at which to start the search.

Return Value:
A MatchCollection of the Match objects found by the search.
Remarks
The Matches methods are similar to the Match methods, except they return the list of successful matches that would result from iteratively calling match= Match (...), then Match.NextMatch (), and so on. The collection includes only successful matches and terminates at the first unsuccessful match.

Return to top


Overloaded Method: Matches(
   string input,
   string pattern
)
Summary
Searches the specified input string for all occurrences of the regular expression specified in the pattern parameter.
C# Syntax:
public static MatchCollection Matches(
   string input,
   string pattern
);
Parameters:

input

The string to search for a match.

pattern

The regular expression pattern to match.

Return Value:
The MatchCollection of Match objects found by the search.
Remarks
The Matches methods are similar to the Match methods, except they return the list of successful matches that would result from iteratively calling match= Match (...), then Match.NextMatch (), and so on. The collection includes only successful matches and terminates at the first unsuccessful match.

The static Matches methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Matches. The static methods are provided to allow an isolated, single use of a regular expression without explicitly creating a Regex object.

Return to top


Overloaded Method: Matches(
   string input,
   string pattern,
   RegexOptions options
)
Summary
Searches the specified input string for all occurrences of the regular expression supplied in a pattern parameter with matching options supplied in an options parameter.
C# Syntax:
public static MatchCollection Matches(
   string input,
   string pattern,
   RegexOptions options
);
Parameters:

input

The string to search for a match.

pattern

The regular expression pattern to match.

options

A bitwise OR combination of RegexOption enumeration values.

Return Value:
The MatchCollection of Match objects found by the search.
Remarks
The Matches methods are similar to the Match methods, except they return the list of successful matches that would result from iteratively calling match= Match (...), then Match.NextMatch (), and so on. The collection includes only successful matches and terminates at the first unsuccessful match.

The static Matches methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Matches. The static methods are provided to allow an isolated, single use of a regular expression without explicitly creating a Regex object.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: Replace(
   string input,
   MatchEvaluator evaluator
)
Summary
Replaces all occurrences of a character pattern defined by the regular expression specified in the Regex constructor . A MatchEvaluator delegate is called at each match to evaluate the replacement.
C# Syntax:
public string Replace(
   string input,
   MatchEvaluator evaluator
);
Parameters:

input

The string to modify.

evaluator

The MatchEvaluator which evaluates replacement at each step.

Return Value:
The modified character string.
Remarks
The MatchEvaluator type is a delegate that takes a single Match as input and returns a string. It is declared as follows:

public delegate String RegexMatchEvaluator(Match match);

The delegate is called once per match during a replace.

Return to top


Overloaded Method: Replace(
   string input,
   string replacement
)
Summary
Replaces all occurrences of a specified regular expression pattern with a replacement string, starting at the first character in the input string.
C# Syntax:
public string Replace(
   string input,
   string replacement
);
Parameters:

input

The string to modify.

replacement

The replacement string.

Return Value:
The modified character string.

Return to top


Overloaded Method: Replace(
   string input,
   MatchEvaluator evaluator,
   int count
)
Summary
Replaces up to a specified number of occurrences of a pattern defined by the regular expression specified in the Regex constructor with a replacement string, starting at the first character in the input string. A MatchEvaluator delegate is called at each match to evaluate the replacement.
C# Syntax:
public string Replace(
   string input,
   MatchEvaluator evaluator,
   int count
);
Parameters:

input

The string to be modified.

evaluator

The MatchEvaluator which evaluates replacement at each step.

count

The maximum number of times the replacement will occur.

Return Value:
The modified character string.
Remarks
The MatchEvaluator type is a delegate that takes a single Match as input and returns a string. It is declared as follows:

public delegate String RegexMatchEvaluator(Match match);

The delegate is called once per match during a replace.

Return to top


Overloaded Method: Replace(
   string input,
   string replacement,
   int count
)
Summary
Replaces up to a specified number of occurrences of a pattern defined by the regular expression specified in the Regex constructor with a specified replacement string, starting at the first character in the input string.
C# Syntax:
public string Replace(
   string input,
   string replacement,
   int count
);
Parameters:

input

String to modify.

replacement

The replacement string.

count

The maximum number of times the replacement can occur.

Return Value:
The modified character string.
Remarks
If count is negative, replacements continue to the end of the string.

Return to top


Overloaded Method: Replace(
   string input,
   string pattern,
   MatchEvaluator evaluator
)
Summary
Replaces all occurrences of a character pattern defined by a regular expression with a replacement character string starting at the first character . A MatchEvaluator delegate is called at each match to evaluate the replacement.
C# Syntax:
public static string Replace(
   string input,
   string pattern,
   MatchEvaluator evaluator
);
Parameters:

input

The string to modify.

pattern

The regular expression pattern to match.

evaluator

The MatchEvaluator which evaluates replacement at each step.

Return Value:
The modified character string.
Remarks
The MatchEvaluator type is a delegate that takes a single Match as input and returns a string. It is declared as follows:

public delegate String RegexMatchEvaluator(Match match);

The delegate is called once per match during a replace.

Return to top


Overloaded Method: Replace(
   string input,
   string pattern,
   string replacement
)
Summary
Replaces all occurrences of matches defined by the regular expression with a replacement string, starting at the first character in the input string.
C# Syntax:
public static string Replace(
   string input,
   string pattern,
   string replacement
);
Parameters:

input

The string to modify.

pattern

The regular expression pattern to match.

replacement

The replacement string.

Return Value:
The modified character string.
Remarks
Substitutions are allowed only within a replacement pattern. For similar functionality within a regular expression, use a backreference such as \1.

Character escapes and substitutions are the only special constructs recognized in a replacement pattern. All other syntactic constructs are allowed in regular expressions only and not recognized in replacement patterns. For example, the replacement pattern a*${test}b inserts the string "a*" followed by the substring matched by the "test" capturing group, if any, followed by the string "b". The * character is not recognized as a metacharacter within a replacement pattern. Similarly, $-patterns are not recognized within a regular expression matching pattern. Within a regular expression, $ denotes the end of the string. Other examples are: $123 substitutes the last substring matched by group number 123 (decimal), and ${name) substitutes the last substring matched by a (?<name>) group.

The static Replace methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Replace. The static methods are provided to allow an isolated, single use of a regular expression without explicitly creating a Regex object.

Return to top


Overloaded Method: Replace(
   string input,
   MatchEvaluator evaluator,
   int count,
   int startat
)
Summary
Replaces up to a specified number of occurrences of a pattern specified in the Regex constructor with a replacement string, starting at a specified character position in the input string. A MatchEvaluator delegate is called at each match to evaluate the replacement.
C# Syntax:
public string Replace(
   string input,
   MatchEvaluator evaluator,
   int count,
   int startat
);
Parameters:

input

The string to be modified.

evaluator

The MatchEvaluator which evaluates replacement at each step.

count

The maximum number of times the replacement will occur.

startat

The character position in the input string where the search will begin.

Return Value:
The modified character string.
Remarks
The MatchEvaluator type is a delegate that takes a single Match as input and returns a string. It is declared as follows:

public delegate String RegexMatchEvaluator(Match match);

The delegate is called once per match during a replace.

Return to top


Overloaded Method: Replace(
   string input,
   string replacement,
   int count,
   int startat
)
Summary
Replaces up to a specified number of occurrences of a pattern in the input string defined by the regular expression specified in the Regex constructor with a specified replacement string, starting at a specified character position in the input string.
C# Syntax:
public string Replace(
   string input,
   string replacement,
   int count,
   int startat
);
Parameters:

input

The string to modify.

replacement

The replacement string.

count

Maximum number of times the replacement can occur.

startat

The character position in the input string where the search will begin.

Return Value:
The modified character string.
Remarks
If count is negative, replacements continue to the end of the string. The other two forms of replace simply supply default values for count (-1) and startat (zero when left-to-right, or input.Length when right-to-left).

Return to top


Overloaded Method: Replace(
   string input,
   string pattern,
   MatchEvaluator evaluator,
   RegexOptions options
)
Summary
Replaces all occurrences of a character pattern defined by a specified regular expression with a replacement character string starting at the first character . Options can be specified to modify matching behavior and a MatchEvaluator delegate is called at each match to evaluate the replacement.
C# Syntax:
public static string Replace(
   string input,
   string pattern,
   MatchEvaluator evaluator,
   RegexOptions options
);
Parameters:

input

The string to modify.

pattern

The regular expression pattern to match.

evaluator

The MatchEvaluator which evaluates replacement at each step.

options

A bitwise OR combination of RegexOption enumeration values.

Return Value:
The modified character string.
Remarks
The MatchEvaluator type is a delegate that takes a single Match as input and returns a string. It is declared as follows:

public delegate String RegexMatchEvaluator(Match match);

The delegate is called once per match during a replace.

Return to top


Overloaded Method: Replace(
   string input,
   string pattern,
   string replacement,
   RegexOptions options
)
Summary
Replaces all occurrences of a pattern defined by a specified regular expression with a specified replacement character string, starting at the first character in the input string. Options can be specified to modify matching behavior.
C# Syntax:
public static string Replace(
   string input,
   string pattern,
   string replacement,
   RegexOptions options
);
Parameters:

input

The string to modify.

pattern

The regular expression pattern to match.

replacement

The replacement string.

options

A bitwise OR combination of RegexOption enumeration values.

Return Value:
The modified string.
Remarks
The static Replace methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Replace. The static methods are provided to allow an isolated, single use of a regular expression without explicitly creating a Regex object.

Return to top


Overloaded Method: Split(
   string input
)
Summary
Splits the specified input string at the positions defined by a regular expression pattern specified in the Regex constructor..
C# Syntax:
public string[] Split(
   string input
);
Parameters:

input

The string to split.

Return Value:
An array of strings.
Remarks
The Regex.Split methods are similar to the String.Split method, except this method splits the string at a delimiter determined by a regular expression instead of a set of characters. If count is specified, the string is split into at most count strings (the last string contains the unsplit remainder of the string); a count value of zero provides the default behavior of splitting as many times as possible. If startat is specified, the first delimiter is searched for starting at the specified point (this can be used, for example, for skipping leading white space).

If capturing groups are used in a Regex.Split expression, the capturing groups are included in the resulting string array. The following example would yield the array items "one", "-", "two", "-", "banana".

              				Regex r = new Regex("(-)"); // Split on hyphens.
              				string[] s = r.Split("one-two-banana");
              			
            

If the regular expression can match the empty string (for example, x*), Split will split the string into an array of single-character strings because the empty string delimiter can be found at every location.

The static Split methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Split. The static methods are provided to allow an isolated, single use of a regular expression without explicitly creating a Regex object.

Return to top


Overloaded Method: Split(
   string input,
   int count
)
Summary
Splits the specified input string a specified maximum number of times at the positions defined by a regular expression specified in the Regex constructor.
C# Syntax:
public string[] Split(
   string input,
   int count
);
Parameters:

input

The string to be split.

count

The maximum number of array elements to return.

Return Value:
An array of strings.
Remarks
The Regex.Split methods are similar to the String.Split method, except this method splits the string at a delimiter determined by a regular expression instead of a set of characters. If count is specified, the string is split into at most count strings (the last string containing the unsplit remainder of the string); a count value of zero provides the default behavior of splitting as many times as possible. If startat is specified, the first delimiter is searched for starting at the specified point (this can be used, for example, for skipping leading white space).

Also, if capturing groups are used in a split expression, these are included in the array of split strings. For example:

              				Regex r = new Regex("(-)"); // Split on hyphens.
              				string[] s = r.Split("one-two-banana");
              			
            

would yield the following array items:"one", "-", "two", "-", "banana"

If the regular expression can match the empty string (for example, x*), Split will split the string into an array of single-character strings because the empty string delimiter can be found at every location.

The static Split methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Split. The static methods are provided to allow an isolated, single use of a regular expression without explicitly creating a Regex object.

Return to top


Overloaded Method: Split(
   string input,
   string pattern
)
Summary
Splits the input string at the positions defined by a regular expression pattern.
C# Syntax:
public static string[] Split(
   string input,
   string pattern
);
Parameters:

input

The string to split.

pattern

The regular expression to match.

Return Value:
An array of strings.
Remarks
The Regex.Split methods are similar to the String.Split method, except this method splits the string at a delimiter determined by a regular expression instead of a set of characters. If count is specified, the string is split into at most count strings (the last string containing the unsplit remainder of the string); a count value of zero provides the default behavior of splitting as many times as possible. If startat is specified, the first delimiter is searched for starting at the specified point (this can be used, for example, for skipping leading white space).

If capturing groups are used in a Regex.Split expression, the capturing groups are included in the resulting string array. The following example would yield the array items "one", "-", "two", "-", "banana".

              				Regex r = new Regex("(-)"); // Split on hyphens. 
              				string[] s = r.Split("one-two-banana");
              			
            

If the regular expression can match the empty string (for example, x*), Split will split the string into an array of single-character strings because the empty string delimiter can be found at every location.

The static Split methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Split. The static methods are provided to allow an isolated, single use of a regular expression without explicitly creating a Regex object.

Return to top


Overloaded Method: Split(
   string input,
   int count,
   int startat
)
Summary
Splits the specified input string a specified maximum number of times at the positions defined by a regular expression specified in the Regex constructor, starting at a specified character position in the input string
C# Syntax:
public string[] Split(
   string input,
   int count,
   int startat
);
Parameters:

input

The string to be split.

count

The maximum number of array elements to return.

startat

The character position in the input string where the search will begin.

Return Value:
An array of strings.
Remarks
The Regex.Split methods are similar to the String.Split method, except this method splits the string at a delimiter determined by a regular expression instead of a set of characters. If count is specified, the string is split into at most count strings (the last string containing the unsplit remainder of the string); a count value of zero provides the default behavior of splitting as many times as possible. If startat is specified, the first delimiter is searched for starting at the specified point (this can be used, for example, for skipping leading white space).

Also, if capturing groups are used in a split expression, these are included in the array of split strings. For example:

              				Regex r = new Regex("(-)"); // Split on hyphens.
              				string[] s = r.Split("one-two-banana");
              			
            

would yield the following array items:"one", "-", "two", "-", "banana"

If the regular expression can match the empty string (for example, x*), Split will split the string into an array of single-character strings because the empty string delimiter can be found at every location.

The static Split methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Split. The static methods are provided to allow an isolated, single use of a regular expression without explicitly creating a Regex object.

Return to top


Overloaded Method: Split(
   string input,
   string pattern,
   RegexOptions options
)
Summary
Splits the input string at the positions defined by a specified regular expression pattern. Options can be specified to modify matching behavior.
C# Syntax:
public static string[] Split(
   string input,
   string pattern,
   RegexOptions options
);
Parameters:

input

The string to split.

pattern

The regular expression defining to match.

options

A bitwise OR combination of RegexOption enumeration values.

Return Value:
An array of strings.
Remarks
The Regex.Split methods are similar to the String.Split method, except this method splits the string at a delimiter determined by a regular expression instead of a set of characters. If count is specified, the string is split into at most count strings (the last string containing the unsplit remainder of the string); a count value of zero provides the default behavior of splitting as many times as possible. If startat is specified, the first delimiter is searched for starting at the specified point (this can be used, for example, for skipping leading white space).

If capturing groups are used in a Regex.Split expression, the capturing groups are included in the resulting string array. The following example would yield the array items "one", "-", "two", "-", "banana".

              				Regex r = new Regex("(-)"); // Split on hyphens.
              				string[] s = r.Split("one-two-banana");
              			
            

If the regular expression can match the empty string (for example, x*), Split will split the string into an array of single-character strings because the empty string delimiter can be found at every location.

The static Split methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Split. The static methods are provided to allow an isolated, single use of a regular expression without explicitly creating a Regex object.

Return to top


Overridden Method: ToString()
Summary
Returns the regular expression pattern that was passed into the Regex constructor.
C# Syntax:
public override string ToString();
Return Value:
The pattern parameter that was passed into the Regex constructor.

Return to top


Method: Unescape(
   string str
)
Summary
Unescapes any escaped characters in the input string.
C# Syntax:
public static string Unescape(
   string str
);
Parameters:

str

The input string containing the text to convert.

Return Value:
A string of characters with any escaped characters converted to their unescaped form.
Remarks
As an example, Unescape replaces \x61 with a and \n with a new-line character (ASCII 10). Unescape reverses the transformation done by Escape. (Of course, Escape cannot perfectly reverse Unescape because it cannot deduce from an unescaped string which characters may have been previously escaped.)

Return to top


Method: UseOptionC()
Summary
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
protected bool UseOptionC();

Return to top


Method: UseOptionR()
Summary
This type supports the Shared Source CLI infrastructure and is not intended to be used directly from your code.
C# Syntax:
protected bool UseOptionR();

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.