Match.Result(String) 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回对指定替换模式的扩展。
public:
 virtual System::String ^ Result(System::String ^ replacement);
	public virtual string Result (string replacement);
	abstract member Result : string -> string
override this.Result : string -> string
	Public Overridable Function Result (replacement As String) As String
	参数
- replacement
 - String
 
要使用的替换模式。
返回
replacement 参数的扩展版本。
例外
replacement 为 null。
不允许对此模式进行扩展。
示例
以下示例将开头和结束括号表达式的连字符替换为括号。
using System;
using System.Text.RegularExpressions;
public class Example
{
   public static void Main()
   {
      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);
         Console.WriteLine(result);
      }
   }
}
// The example displays the following output:
//       (decisively)
//       (whatever time it was)
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)
正则表达式模式 --(.+?)-- 的含义如下表所示。
| 模式 | 说明 | 
|---|---|
-- | 
匹配两个连字符。 | 
(.+?) | 
匹配任意字符一次或多次,但尽可能少。 这是第一个捕获组。 | 
-- | 
匹配两个连字符。 | 
请注意,正则表达式模式 --(.+?)-- 使用惰性限定符 +?。 如果改用贪婪限定符 + ,则正则表达式引擎只会在输入字符串中找到单个匹配项。
替换字符串 ($1) 将匹配项替换为第一个捕获的组,该组括在括号中。
注解
虽然该方法用 Regex.Replace 指定的替换模式替换输入字符串中的所有匹配项,但该方法将 Result 单个匹配项替换为指定的替换模式。 由于它在单个匹配项上操作,因此也可以在调用 Result 该方法之前对匹配的字符串执行处理。
该 replacement 参数是标准正则表达式替换模式。 它可以由文本字符和正则表达式替换组成。 有关更多信息,请参见 替代。