Match.Result(String) 方法


傳回所指定取代模式的展開 (Expansion)。Returns the expansion of the specified replacement pattern.

 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



要使用的取代模式。The replacement pattern to use.



replacement 參數的展開版本。The expanded version of the replacement parameter.


replacementnullreplacement is null.

這種模式不允許展開。Expansion is not allowed for this pattern.


下列範例會取代開頭和結尾加上括弧之運算式的連字號。The following example replaces the hyphens that begin and end a parenthetical expression with parentheses.

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);
// 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)
   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.

模式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.

請注意,正則運算式模式會 --(.+?)-- 使用 lazy 數量詞 +?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.

取代字串 ($1) 會將相符項取代為第一個已捕捉的群組,並以括弧括住。The replacement string ($1) replaces the match with the first captured group, which is enclosed in parentheses.


方法會以 Regex.Replace 指定的取代模式取代輸入字串中的所有相符專案,而 Result 方法則會以指定的取代模式取代單一相符專案。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. 因為它會在個別的相符項上運作,所以在呼叫方法之前,也可以在相符的字串上執行處理 ResultBecause it operates on an individual match, it is also possible to perform processing on the matched string before you call the Result method.

replacement參數是標準正則運算式取代模式。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.