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

Regex.Replace Method (String, String, MatchEvaluator, RegexOptions, TimeSpan)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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.

Namespace:  System.Text.RegularExpressions
Assembly:  System (in System.dll)

'Declaration
Public Shared Function Replace ( _
	input As String, _
	pattern As String, _
	evaluator As MatchEvaluator, _
	options As RegexOptions, _
	matchTimeout As TimeSpan _
) As String

Parameters

input
Type: System.String
The string to search for a match.
pattern
Type: System.String
The regular expression pattern to match.
evaluator
Type: System.Text.RegularExpressions.MatchEvaluator
A custom method that examines each match and returns either the original matched string or a replacement string.
options
Type: System.Text.RegularExpressions.RegexOptions
A bitwise combination of enumeration values that provide options for matching.
matchTimeout
Type: System.TimeSpan
A time-out interval, or Regex.InfiniteMatchTimeout to indicate that the method should not time out.

Return Value

Type: System.String
A new string that is identical to the input string, except that the replacement string takes the place of each matched string.

ExceptionCondition
ArgumentException

A regular expression parsing error occurred.

ArgumentNullException

input, pattern, or evaluator is Nothing.

ArgumentOutOfRangeException

options is not a valid bitwise combination of RegexOptions values.

RegexMatchTimeoutException

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 method is equivalent to calling the Regex.Matches(String, String, RegexOptions) method and passing each Match object in the returned MatchCollection collection to the evaluator delegate.

The pattern parameter consists of regular expression language elements that symbolically describe the string to match. For more information about regular expressions, see Regular Expression Language - Quick Reference and [930653a6-95d2-4697-9d5a-52d11bb6fd4c].

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.


Public Function MatchEvaluatorMethod(ByVal match As Match) As String


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 [618e5afb-3a97-440d-831a-70e4c526a51c] and [34df1152-0b22-4a1c-a76c-3c28c47b70d8]. 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.

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(Of TKey, TValue)(TKey(), TValue(), IComparer(Of 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 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.

Windows Phone OS

Supported in: 8.1, 8.0

Show:
© 2017 Microsoft