Regex.Replace Method (String, String, String, 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 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.
Assembly: System (in System.dll)
'Declaration Public Shared Function Replace ( _ input As String, _ pattern As String, _ replacement As String, _ 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.
- replacement
- Type: System.String
The replacement string.
- options
- Type: System.Text.RegularExpressions.RegexOptions
A bitwise combination of the 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.StringA new string that is identical to the input string, except that the replacement string takes the place of each matched string.
| Exception | Condition |
|---|---|
| ArgumentException | A regular expression parsing error occurred. |
| ArgumentNullException | input, pattern, or replacement 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 static Replace methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Replace.
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]. 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 replacement parameter specifies the string that is to replace each match in input. replacement can consist of any combination of literal text and [d1f52431-1c7d-4dc6-8792-6b988256892e]. For example, the replacement pattern a*${test}b inserts the string "a*" followed by the substring that is 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.
Note: |
|---|
Substitutions are the only regular expression language elements that are recognized in a replacement pattern. All other regular expression language elements, including [f49cc9cc-db7d-4058-8b8a-422bc08b29b0], are allowed in regular expression patterns only and are not recognized in replacement patterns. |
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 CallersWe 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.
Note: