Returns the expansion of the specified replacement pattern.
Assembly: System (in System.dll)
Whereas the Regex.Replace method replaces all matches in an input string with a specified replacement pattern, the method replaces a single match with a specified replacement pattern. Because it operates on an individual match, it is also possible to perform processing on the matched string before you call the method.
The replacement parameter is a standard regular expression replacement pattern. It can consist of literal characters and regular expression substitutions. For more information, see Substitutions in Regular Expressions.
The following example replaces the hyphens that begin and end a parenthetical expression with parentheses.
Imports System.Text.RegularExpressions Module Example Public Sub Main() Dim pattern As String = "--(.+?)--" Dim replacement As String = "($1)" Dim input As String = "He said--decisively--that the time--whatever time it was--had come." For Each match As Match In Regex.Matches(input, pattern) Dim result As String = match.Result(replacement) Console.WriteLine(result) Next End Sub End Module ' The example displays the following output: ' (decisively) ' (whatever time it was)
The regular expression pattern --(.+?)-- is interpreted as shown in the following table.
Match two hyphens.
Match any character one or more times, but as few times as possible. This is the first capturing group.
Match two hyphens.
Note that the regular expression pattern --(.+?)-- uses the lazy quantifier +?. If the greedy quantifier + were used instead, the regular expression engine would find only a single match in the input string.
The replacement string ($1) replaces the match with the first captured group, which is enclosed in parentheses.
.NET FrameworkSupported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
Portable Class LibrarySupported in: Portable Class Library
.NET for Windows Store appsSupported in: Windows 8
.NET for Windows Phone appsSupported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)