Regex.Replace Method

Definition

In a specified input string, replaces strings that match a regular expression pattern with a specified replacement string.

Overloads

Name Description
Replace(String, String, String, RegexOptions)

In a specified input string, replaces all strings that match a specified regular expression with a specified replacement string. Specified options modify the matching operation.

Replace(String, String, String, RegexOptions, TimeSpan)

In a specified input string, replaces all strings that match a specified regular expression with a specified replacement string. Additional parameters specify options that modify the matching operation and a time-out interval if no match is found.

Replace(String, MatchEvaluator, Int32, Int32)

In a specified input substring, replaces a specified maximum number of strings that match a regular expression pattern with a string returned by a MatchEvaluator delegate.

Replace(String, String, MatchEvaluator, RegexOptions)

In a specified input string, replaces all strings that match a specified regular expression with a string returned by a MatchEvaluator delegate. Specified options modify the matching operation.

Replace(String, String, MatchEvaluator, RegexOptions, TimeSpan)

In a specified input string, replaces all substrings that match a specified regular expression with a string returned by a MatchEvaluator delegate. Additional parameters specify options that modify the matching operation and a time-out interval if no match is found.

Replace(String, String, Int32, Int32)

In a specified input substring, replaces a specified maximum number of strings that match a regular expression pattern with a specified replacement string.

Replace(String, String, String)

In a specified input string, replaces all strings that match a specified regular expression pattern with a specified replacement string.

Replace(String, String, MatchEvaluator)

In a specified input string, replaces all strings that match a specified regular expression with a string returned by a MatchEvaluator delegate.

Replace(String, String, Int32)

In a specified input string, replaces a specified maximum number of strings that match a regular expression pattern with a specified replacement string.

Replace(String, MatchEvaluator)

In a specified input string, replaces all strings that match a specified regular expression with a string returned by a MatchEvaluator delegate.

Replace(String, String)

In a specified input string, replaces all strings that match a regular expression pattern with a specified replacement string.

Replace(String, MatchEvaluator, Int32)

In a specified input string, replaces a specified maximum number of strings that match a regular expression pattern with a string returned by a MatchEvaluator delegate.

Replace(String, String, String, RegexOptions)

Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs

In a specified input string, replaces all strings that match a specified regular expression with a specified replacement string. Specified options modify the matching operation.

public:
 static System::String ^ Replace(System::String ^ input, System::String ^ pattern, System::String ^ replacement, System::Text::RegularExpressions::RegexOptions options);
public static string Replace(string input, string pattern, string replacement, System.Text.RegularExpressions.RegexOptions options);
static member Replace : string * string * string * System.Text.RegularExpressions.RegexOptions -> string
Public Shared Function Replace (input As String, pattern As String, replacement As String, options As RegexOptions) As String

Parameters

input
String

The string to search for a match.

pattern
String

The regular expression pattern to match.

replacement
String

The replacement string.

options
RegexOptions

A bitwise combination of the enumeration values that provide options for matching.

Returns

A new string that is identical to the input string, except that the replacement string takes the place of each matched string. If pattern is not matched in the current instance, the method returns the current instance unchanged.

Exceptions

A regular expression parsing error occurred.

input, pattern, or replacement is null.

options is not a valid bitwise combination of RegexOptions values.

A time-out occurred.

Remarks

The static Replace(String, String, String, RegexOptions) methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Replace(String, String).

The replacement parameter specifies the string that replaces each match in input. replacement can consist of any combination of literal text and substitutions. Substitutions are the only regular expression language elements that are recognized in a replacement pattern.

Because the method returns input unchanged if there is no match, you can use the ReferenceEquals(Object, Object) method to determine whether the method has made any replacements.

If you specify RightToLeft for the options parameter, the search for matches begins at the end of the input string and moves left; otherwise, the search begins at the start of the input string and moves right.

Notes to Callers

This method times out after an interval that is equal to the default time-out value of the application domain in which it is called. If a time-out value has not been defined for the application domain, the value InfiniteMatchTimeout, which prevents the method from timing out, is used. The recommended static method for replacing a pattern match is Replace(String, String, String, RegexOptions, TimeSpan), which lets you set the time-out interval.

See also

Applies to

Replace(String, String, String, RegexOptions, TimeSpan)

Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs

In a specified input string, replaces all strings that match a specified regular expression with a specified replacement string. Additional parameters specify options that modify the matching operation and a time-out interval if no match is found.

public:
 static System::String ^ Replace(System::String ^ input, System::String ^ pattern, System::String ^ replacement, System::Text::RegularExpressions::RegexOptions options, TimeSpan matchTimeout);
public static string Replace(string input, string pattern, string replacement, System.Text.RegularExpressions.RegexOptions options, TimeSpan matchTimeout);
static member Replace : string * string * string * System.Text.RegularExpressions.RegexOptions * TimeSpan -> string
Public Shared Function Replace (input As String, pattern As String, replacement As String, options As RegexOptions, matchTimeout As TimeSpan) As String

Parameters

input
String

The string to search for a match.

pattern
String

The regular expression pattern to match.

replacement
String

The replacement string.

options
RegexOptions

A bitwise combination of the enumeration values that provide options for matching.

matchTimeout
TimeSpan

A time-out interval, or InfiniteMatchTimeout to indicate that the method should not time out.

Returns

A new string that is identical to the input string, except that the replacement string takes the place of each matched string. If pattern is not matched in the current instance, the method returns the current instance unchanged.

Exceptions

A regular expression parsing error occurred.

input, pattern, or replacement is null.

options is not a valid bitwise combination of RegexOptions values. -or- matchTimeout is negative, zero, or greater than approximately 24 days.

A time-out occurred.

Remarks

The static Replace(String, String, String, RegexOptions, TimeSpan) methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Replace(String, String).

The replacement parameter specifies the string that replaces each match in input. replacement can consist of any combination of literal text and substitutions. Substitutions are the only regular expression language elements that are recognized in a replacement pattern.

Because the method returns input unchanged if there is no match, you can use the ReferenceEquals(Object, Object) method to determine whether the method has made any replacements.

If you specify RightToLeft for the options parameter, the search for matches begins at the end of the input string and moves left; otherwise, the search begins at the start of the input string and moves right.

The matchTimeout parameter specifies how long a pattern matching method should try to find a match before it times out. Setting a time-out interval prevents regular expressions that rely on excessive backtracking from appearing to stop responding when they process input that contains near matches. matchTimeout overrides any default time-out value defined for the application domain in which the method executes.

Notes to Callers

We recommend that you set the matchTimeout parameter to an appropriate value, such as two seconds. If you disable time-outs by specifying InfiniteMatchTimeout, the regular expression engine offers slightly better performance. However, you should disable time-outs only under the following conditions:

  • When the input processed by a regular expression is derived from a known and trusted source or consists of static text. This excludes text that has been dynamically input by users.

  • When the regular expression pattern has been thoroughly tested to ensure that it efficiently handles matches, non-matches, and near matches.

  • When the regular expression pattern contains no language elements that are known to cause excessive backtracking when processing a near match.

See also

Applies to

Replace(String, MatchEvaluator, Int32, Int32)

Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs

In a specified input substring, replaces a specified maximum number of strings that match a regular expression pattern with a string returned by a MatchEvaluator delegate.

public:
 System::String ^ Replace(System::String ^ input, System::Text::RegularExpressions::MatchEvaluator ^ evaluator, int count, int startat);
public string Replace(string input, System.Text.RegularExpressions.MatchEvaluator evaluator, int count, int startat);
member this.Replace : string * System.Text.RegularExpressions.MatchEvaluator * int * int -> string
Public Function Replace (input As String, evaluator As MatchEvaluator, count As Integer, startat As Integer) As String

Parameters

input
String

The string to search for a match.

evaluator
MatchEvaluator

A custom method that examines each match and returns either the original matched string or a replacement string.

count
Int32

The maximum number of times the replacement will occur.

startat
Int32

The character position in the input string where the search begins.

Returns

A new string that is identical to the input string, except that a replacement string takes the place of each matched string. If the regular expression pattern is not matched in the current instance, the method returns the current instance unchanged.

Exceptions

input or evaluator is null.

startat is less than zero or greater than the length of input.

A time-out occurred.

Remarks

For more details about startat, see the Remarks section of Match(String, Int32).

The method passes the first countMatch objects to the evaluator delegate.

See also

Applies to

Replace(String, String, MatchEvaluator, RegexOptions)

Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs

In a specified input string, replaces all strings that match a specified regular expression with a string returned by a MatchEvaluator delegate. Specified options modify the matching operation.

public:
 static System::String ^ Replace(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::MatchEvaluator ^ evaluator, System::Text::RegularExpressions::RegexOptions options);
public static string Replace(string input, string pattern, System.Text.RegularExpressions.MatchEvaluator evaluator, System.Text.RegularExpressions.RegexOptions options);
static member Replace : string * string * System.Text.RegularExpressions.MatchEvaluator * System.Text.RegularExpressions.RegexOptions -> string
Public Shared Function Replace (input As String, pattern As String, evaluator As MatchEvaluator, options As RegexOptions) As String

Parameters

input
String

The string to search for a match.

pattern
String

The regular expression pattern to match.

evaluator
MatchEvaluator

A custom method that examines each match and returns either the original matched string or a replacement string.

options
RegexOptions

A bitwise combination of the enumeration values that provide options for matching.

Returns

A new string that is identical to the input string, except that a replacement string takes the place of each matched string. If pattern is not matched in the current instance, the method returns the current instance unchanged.

Exceptions

A regular expression parsing error occurred.

input, pattern, or evaluator is null.

options is not a valid bitwise combination of RegexOptions values.

A time-out occurred.

Remarks

This method is useful for replacing a regular expression match if any of the following conditions is true: the replacement string cannot readily be specified by a regular expression replacement pattern, the replacement string results from processing the matched string, or the replacement string results from conditional processing.

The method is equivalent to calling the Matches(String, String) method and passing each Match object in the returned MatchCollection to the evaluator delegate.

Because the method returns input unchanged if there is no match, you can use the ReferenceEquals(Object, Object) method to determine whether the method has made any replacements.

If you specify RightToLeft for the options parameter, the search for matches begins at the end of the input string and moves left; otherwise, the search begins at the start of the input string and moves right.

See also

Applies to

Replace(String, String, MatchEvaluator, RegexOptions, TimeSpan)

Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs

In a specified input string, replaces all substrings that match a specified regular expression with a string returned by a MatchEvaluator delegate. Additional parameters specify options that modify the matching operation and a time-out interval if no match is found.

public:
 static System::String ^ Replace(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::MatchEvaluator ^ evaluator, System::Text::RegularExpressions::RegexOptions options, TimeSpan matchTimeout);
public static string Replace(string input, string pattern, System.Text.RegularExpressions.MatchEvaluator evaluator, System.Text.RegularExpressions.RegexOptions options, TimeSpan matchTimeout);
static member Replace : string * string * System.Text.RegularExpressions.MatchEvaluator * System.Text.RegularExpressions.RegexOptions * TimeSpan -> string
Public Shared Function Replace (input As String, pattern As String, evaluator As MatchEvaluator, options As RegexOptions, matchTimeout As TimeSpan) As String

Parameters

input
String

The string to search for a match.

pattern
String

The regular expression pattern to match.

evaluator
MatchEvaluator

A custom method that examines each match and returns either the original matched string or a replacement string.

options
RegexOptions

A bitwise combination of the enumeration values that provide options for matching.

matchTimeout
TimeSpan

A time-out interval, or InfiniteMatchTimeout to indicate that the method should not time out.

Returns

A new string that is identical to the input string, except that a replacement string takes the place of each matched string. If pattern is not matched in the current instance, the method returns the current instance unchanged.

Exceptions

A regular expression parsing error occurred.

input, pattern, or evaluator is null.

options is not a valid bitwise combination of RegexOptions values. -or- matchTimeout is negative, zero, or greater than approximately 24 days.

A time-out occurred.

Remarks

This method is useful for replacing a regular expression match if any of the following conditions is true: the replacement string cannot readily be specified by a regular expression replacement pattern, the replacement string results from processing the matched string, or the replacement string results from conditional processing.

The method is equivalent to calling the Matches(String, String) method and passing each Match object in the returned MatchCollection to the evaluator delegate.

Because the method returns input unchanged if there is no match, you can use the ReferenceEquals(Object, Object) method to determine whether the method has made any replacements.

If you specify RightToLeft for the options parameter, the search for matches begins at the end of the input string and moves left; otherwise, the search begins at the start of the input string and moves right.

The matchTimeout parameter specifies how long a pattern matching method should try to find a match before it times out. matchTimeout overrides any default time-out value defined for the application domain in which the method executes.

Notes to Callers

We recommend that you set the matchTimeout parameter to an appropriate value, such as two seconds. If you disable time-outs by specifying InfiniteMatchTimeout, the regular expression engine offers slightly better performance. However, you should disable time-outs only under the following conditions:

  • When the input processed by a regular expression is derived from a known and trusted source or consists of static text. This excludes text that has been dynamically input by users.

  • When the regular expression pattern has been thoroughly tested to ensure that it efficiently handles matches, non-matches, and near matches.

  • When the regular expression pattern contains no language elements that are known to cause excessive backtracking when processing a near match.

See also

Applies to

Replace(String, String, Int32, Int32)

Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs

In a specified input substring, replaces a specified maximum number of strings that match a regular expression pattern with a specified replacement string.

public:
 System::String ^ Replace(System::String ^ input, System::String ^ replacement, int count, int startat);
public string Replace(string input, string replacement, int count, int startat);
member this.Replace : string * string * int * int -> string
Public Function Replace (input As String, replacement As String, count As Integer, startat As Integer) As String

Parameters

input
String

The string to search for a match.

replacement
String

The replacement string.

count
Int32

The maximum number of times the replacement can occur.

startat
Int32

The character position in the input string where the search begins.

Returns

A new string that is identical to the input string, except that the replacement string takes the place of each matched string. If the regular expression pattern is not matched in the current instance, the method returns the current instance unchanged.

Exceptions

input or replacement is null.

startat is less than zero or greater than the length of input.

A time-out occurred.

Remarks

For more details about startat, see the Remarks section of Match(String, Int32).

The replacement parameter specifies the string that replaces each match and supports substitutions.

If count is negative, replacements continue to the end of the string.

See also

Applies to

Replace(String, String, String)

Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs

In a specified input string, replaces all strings that match a specified regular expression pattern with a specified replacement string.

public:
 static System::String ^ Replace(System::String ^ input, System::String ^ pattern, System::String ^ replacement);
public static string Replace(string input, string pattern, string replacement);
static member Replace : string * string * string -> string
Public Shared Function Replace (input As String, pattern As String, replacement As String) As String

Parameters

input
String

The string to search for a match.

pattern
String

The regular expression pattern to match.

replacement
String

The replacement string.

Returns

A new string that is identical to the input string, except that the replacement string takes the place of each matched string. If pattern is not matched in the current instance, the method returns the current instance unchanged.

Exceptions

A regular expression parsing error occurred.

input, pattern, or replacement is null.

A time-out occurred.

Remarks

The static Replace(String, String, String) methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Replace(String, String).

The replacement parameter specifies the string that replaces each match in input. replacement can consist of any combination of literal text and substitutions. Substitutions are the only regular expression language elements that are recognized in a replacement pattern.

Because the method returns input unchanged if there is no match, you can use the ReferenceEquals(Object, Object) method to determine whether the method has made any replacements.

Notes to Callers

This method times out after an interval that is equal to the default time-out value of the application domain in which it is called. If a time-out value has not been defined for the application domain, the value InfiniteMatchTimeout, which prevents the method from timing out, is used. The recommended static method for replacing a pattern match is Replace(String, String, String, RegexOptions, TimeSpan), which lets you set the time-out interval.

See also

Applies to

Replace(String, String, MatchEvaluator)

Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs

In a specified input string, replaces all strings that match a specified regular expression with a string returned by a MatchEvaluator delegate.

public:
 static System::String ^ Replace(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::MatchEvaluator ^ evaluator);
public static string Replace(string input, string pattern, System.Text.RegularExpressions.MatchEvaluator evaluator);
static member Replace : string * string * System.Text.RegularExpressions.MatchEvaluator -> string
Public Shared Function Replace (input As String, pattern As String, evaluator As MatchEvaluator) As String

Parameters

input
String

The string to search for a match.

pattern
String

The regular expression pattern to match.

evaluator
MatchEvaluator

A custom method that examines each match and returns either the original matched string or a replacement string.

Returns

A new string that is identical to the input string, except that a replacement string takes the place of each matched string. If pattern is not matched in the current instance, the method returns the current instance unchanged.

Exceptions

A regular expression parsing error occurred.

input, pattern, or evaluator is null.

A time-out occurred.

Remarks

This method is useful for replacing a regular expression match if any of the following conditions is true: the replacement string cannot readily be specified by a regular expression replacement pattern, the replacement string results from processing the matched string, or the replacement string results from conditional processing.

The method is equivalent to calling the Matches(String, String) method and passing each Match object in the returned MatchCollection to the evaluator delegate.

Because the method returns input unchanged if there is no match, you can use the ReferenceEquals(Object, Object) method to determine whether the method has made any replacements.

Notes to Callers

This method times out after an interval that is equal to the default time-out value of the application domain in which it is called. If a time-out value has not been defined for the application domain, the value InfiniteMatchTimeout, which prevents the method from timing out, is used. The recommended static method for evaluating and replacing a pattern match is Replace(String, String, MatchEvaluator, RegexOptions, TimeSpan), which lets you set the time-out interval.

See also

Applies to

Replace(String, String, Int32)

Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs

In a specified input string, replaces a specified maximum number of strings that match a regular expression pattern with a specified replacement string.

public:
 System::String ^ Replace(System::String ^ input, System::String ^ replacement, int count);
public string Replace(string input, string replacement, int count);
member this.Replace : string * string * int -> string
Public Function Replace (input As String, replacement As String, count As Integer) As String

Parameters

input
String

The string to search for a match.

replacement
String

The replacement string.

count
Int32

The maximum number of times the replacement can occur.

Returns

A new string that is identical to the input string, except that the replacement string takes the place of each matched string. If the regular expression pattern is not matched in the current instance, the method returns the current instance unchanged.

Exceptions

input or replacement is null.

A time-out occurred.

Remarks

The search begins at the start of the input string. The replacement parameter specifies the string that replaces each match and supports substitutions.

If count is negative, replacements continue to the end of the string.

Because the method returns input unchanged if there is no match, you can use the ReferenceEquals(Object, Object) method to determine whether the method has made any replacements.

See also

Applies to

Replace(String, MatchEvaluator)

Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs

In a specified input string, replaces all strings that match a specified regular expression with a string returned by a MatchEvaluator delegate.

public:
 System::String ^ Replace(System::String ^ input, System::Text::RegularExpressions::MatchEvaluator ^ evaluator);
public string Replace(string input, System.Text.RegularExpressions.MatchEvaluator evaluator);
member this.Replace : string * System.Text.RegularExpressions.MatchEvaluator -> string
Public Function Replace (input As String, evaluator As MatchEvaluator) As String

Parameters

input
String

The string to search for a match.

evaluator
MatchEvaluator

A custom method that examines each match and returns either the original matched string or a replacement string.

Returns

A new string that is identical to the input string, except that a replacement string takes the place of each matched string. If the regular expression pattern is not matched in the current instance, the method returns the current instance unchanged.

Exceptions

input or evaluator is null.

A time-out occurred.

Remarks

This method is useful for replacing a regular expression match if any of the following conditions is true: the replacement string cannot readily be specified by a regular expression replacement pattern, the replacement string results from processing the matched string, or the replacement string results from conditional processing.

The method is equivalent to calling the Matches(String) method and passing each Match object in the returned MatchCollection to the evaluator delegate.

Because the method returns input unchanged if there is no match, you can use the ReferenceEquals(Object, Object) method to determine whether the method has made any replacements.

See also

Applies to

Replace(String, String)

Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs

In a specified input string, replaces all strings that match a regular expression pattern with a specified replacement string.

public:
 System::String ^ Replace(System::String ^ input, System::String ^ replacement);
public string Replace(string input, string replacement);
member this.Replace : string * string -> string
Public Function Replace (input As String, replacement As String) As String

Parameters

input
String

The string to search for a match.

replacement
String

The replacement string.

Returns

A new string that is identical to the input string, except that the replacement string takes the place of each matched string. If the regular expression pattern is not matched in the current instance, the method returns the current instance unchanged.

Exceptions

input or replacement is null.

A time-out occurred.

Remarks

The search for the pattern begins at the beginning of the input string.

The replacement parameter specifies the string that replaces each match. replacement can consist of any combination of literal text and substitutions. Substitutions are the only regular expression language elements that are recognized in a replacement pattern.

Because the method returns input unchanged if there is no match, you can use the ReferenceEquals(Object, Object) method to determine whether the method has made any replacements.

See also

Applies to

Replace(String, MatchEvaluator, Int32)

Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs
Source:
Regex.Replace.cs

In a specified input string, replaces a specified maximum number of strings that match a regular expression pattern with a string returned by a MatchEvaluator delegate.

public:
 System::String ^ Replace(System::String ^ input, System::Text::RegularExpressions::MatchEvaluator ^ evaluator, int count);
public string Replace(string input, System.Text.RegularExpressions.MatchEvaluator evaluator, int count);
member this.Replace : string * System.Text.RegularExpressions.MatchEvaluator * int -> string
Public Function Replace (input As String, evaluator As MatchEvaluator, count As Integer) As String

Parameters

input
String

The string to search for a match.

evaluator
MatchEvaluator

A custom method that examines each match and returns either the original matched string or a replacement string.

count
Int32

The maximum number of times the replacement will occur.

Returns

A new string that is identical to the input string, except that a replacement string takes the place of each matched string. If the regular expression pattern is not matched in the current instance, the method returns the current instance unchanged.

Exceptions

input or evaluator is null.

A time-out occurred.

Remarks

This method is useful for replacing a regular expression match if any of the following conditions is true: the replacement string cannot readily be specified by a regular expression replacement pattern, the replacement string results from processing the matched string, or the replacement string results from conditional processing.

The method is equivalent to calling the Matches(String) method and passing the first countMatch objects in the returned MatchCollection to the evaluator delegate.

If count is negative, replacements continue to the end of the string.

Because the method returns input unchanged if there is no match, you can use the ReferenceEquals(Object, Object) method to determine whether the method has made any replacements.

See also

Applies to