Regex.Replace Method (String, String, String, RegexOptions)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Within a specified input string, replaces all strings that match a specified regular expression with a specified replacement string. Specified options modify the matching operation.
Assembly: System (in System.dll)
'Declaration Public Shared Function Replace ( _ input As String, _ pattern As String, _ replacement As String, _ options As RegexOptions _ ) 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.
Return Value
Type: System.StringA new string that is identical to the input string, except that a replacement string takes the place of each matched string.
| Exception | Condition |
|---|---|
| ArgumentException | A regular expression parsing error occurred. |
| ArgumentNullException | input is Nothing. -or- pattern is Nothing. -or- replacement is Nothing. |
| ArgumentOutOfRangeException | options is not a valid bitwise combination of RegexOptions values. |
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 various regular expression language elements that symbolically describe the string to match. For more information about regular expressions, see Regular Expression Language Elements in the .NET Framework documentation. If the options parameter specifies the RightToLeft enumeration value, the search for matches begins from the end of the input string and proceeds from right to left; otherwise, the search begins from the start of the input string and proceeds from left to 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 substitutions. 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 character escapes, are allowed in regular expression patterns only and are not recognized in replacement patterns. |
Note: