Regex::Replace Method (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.
Assembly: System (in System.dll)
public: static String^ Replace( String^ input, String^ pattern, MatchEvaluator^ evaluator, RegexOptions options, TimeSpan matchTimeout )
The string to search for a match.
The regular expression pattern to match.
A custom method that examines each match and returns either the original matched string or a replacement string.
A bitwise combination of enumeration values that provide options for matching.
Return ValueType: System::String^
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.
A regular expression parsing error occurred.
input, pattern, or evaluator is null.
options is not a valid bitwise combination of RegexOptions values.
matchTimeout is negative, zero, or greater than approximately 24 days.
A time-out occurred. For more information about time-outs, see the Remarks section.
The Regex::Replace(String^, String^, MatchEvaluator^, RegexOptions) method is useful for replacing a regular expression match if any of the following conditions is true:
If the replacement string cannot readily be specified by a regular expression replacement pattern.
If the replacement string results from some processing performed on the matched string.
If the replacement string results from conditional processing.
The pattern parameter consists of regular expression language elements that symbolically describe the string to match. For more information about regular expressions, see .NET Framework Regular Expressions and Regular Expression Language - Quick Reference.
The evaluator parameter is the delegate for a custom method that you define and that examines each match. The custom method must have the following signature to match the MatchEvaluator delegate.
Your custom method returns a string that replaces the matched input.
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. For more information, see Best Practices for Regular Expressions in the .NET Framework and Backtracking in Regular Expressions. If no match is found in that time interval, the method throws a RegexMatchTimeoutException exception. matchTimeout overrides any default time-out value defined for the application domain in which the method executes.
Because the method returns input unchanged if there is no match, you can use the Object::ReferenceEquals method to determine whether the method has made any replacements to the input string.
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 Regex::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.
The following example uses a regular expression to extract the individual words from a string, and then uses a MatchEvaluator delegate to call a method named WordScramble that scrambles the individual letters in the word. To do this, the WordScramble method creates an array that contains the characters in the match. It also creates a parallel array that it populates with random floating-point numbers. The arrays are sorted by calling the Array::Sort<TKey, TValue>(array<TKey>^, array<TValue>^, IComparer<TKey>^) method, and the sorted array is provided as an argument to a String class constructor. This newly created string is then returned by the WordScramble method. The regular expression pattern \w+ matches one or more word characters; the regular expression engine will continue to add characters to the match until it encounters a non-word character, such as a white-space character. The call to the Replace(String^, String^, MatchEvaluator^, RegexOptions) method includes the RegexOptions::IgnorePatternWhitespace option so that the comment in the regular expression pattern \w+ # Matches all the characters in a word. is ignored by the regular expression engine.
Available since 4.5
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Available since 8.1