Match.Result Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Returns the expansion of the specified replacement pattern.
Namespace: System.Text.RegularExpressions
Assembly: System (in System.dll)
Syntax
'Declaration
Public Overridable Function Result ( _
replacement As String _
) As String
public virtual string Result(
string replacement
)
Parameters
- replacement
Type: System.String
The replacement pattern to use.
Return Value
Type: System.String
The expanded version of the replacement parameter.
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | replacement is nulla null reference (Nothing in Visual Basic). |
NotSupportedException | Expansion is not allowed for this pattern. |
Remarks
Whereas the Regex.Replace method replaces all matches in an input string with a specified replacement pattern, the Result 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 Result 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 the full .NET Framework documentation.
Examples
The following example replaces the hyphens that begin and end a parenthetical expression with parentheses.
Imports System.Text.RegularExpressions
Module Example
Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
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)
outputBlock.Text &= result & vbCrLf
Next
End Sub
End Module
' The example displays the following output:
' (decisively)
' (whatever time it was)
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
string pattern = "--(.+?)--";
string replacement = "($1)";
string input = "He said--decisively--that the time--whatever time it was--had come.";
foreach (Match match in Regex.Matches(input, pattern))
{
string result = match.Result(replacement);
outputBlock.Text += result + "\n";
}
}
}
// The example displays the following output:
// (decisively)
// (whatever time it was)
The regular expression pattern --(.+?)-- is interpreted as shown in the following table.
Pattern |
Description |
---|---|
-- |
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.
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
See Also