Match.Result(String) Método
Definição
Retorna a expansão do padrão de substituição especificado.Returns the expansion of the specified replacement pattern.
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
Parâmetros
- replacement
- String
O padrão de substituição a ser usado.The replacement pattern to use.
Retornos
A versão expandida do parâmetro replacement.The expanded version of the replacement parameter.
Exceções
replacement é null.replacement is null.
A expansão não é permitida para este padrão.Expansion is not allowed for this pattern.
Exemplos
O exemplo a seguir substitui os hifens que começam e terminam uma expressão entre parênteses.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);
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)
O padrão da expressão regular --(.+?)-- é interpretado conforme mostrado na tabela a seguir.The regular expression pattern --(.+?)-- is interpreted as shown in the following table.
| PadrãoPattern | DescriçãoDescription |
|---|---|
-- |
Corresponder dois hifens.Match two hyphens. |
(.+?) |
Corresponder qualquer caractere uma ou mais vezes, mas tantas vezes quanto possível.Match any character one or more times, but as few times as possible. Este é o primeiro grupo de captura.This is the first capturing group. |
-- |
Corresponder dois hifens.Match two hyphens. |
Observe que o padrão de expressão regular --(.+?)-- usa o quantificador lento +? .Note that the regular expression pattern --(.+?)-- uses the lazy quantifier +?. Se o quantificador de ávidos + fosse usado em vez disso, o mecanismo de expressão regular localizaria apenas uma única correspondência na cadeia de caracteres de entrada.If the greedy quantifier + were used instead, the regular expression engine would find only a single match in the input string.
A cadeia de caracteres de substituição ($1) substitui a correspondência pelo primeiro grupo capturado, que é colocado entre parênteses.The replacement string ($1) replaces the match with the first captured group, which is enclosed in parentheses.
Comentários
Enquanto o Regex.Replace método substitui todas as correspondências em uma cadeia de caracteres de entrada por um padrão de substituição especificado, o Result método substitui uma correspondência única por um padrão de substituição especificado.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. Como ele opera em uma correspondência individual, também é possível executar o processamento na cadeia de caracteres correspondente antes de chamar o Result método.Because it operates on an individual match, it is also possible to perform processing on the matched string before you call the Result method.
O replacement parâmetro é um padrão de substituição de expressão regular padrão.The replacement parameter is a standard regular expression replacement pattern. Ele pode consistir em caracteres literais e substituições de expressão regular.It can consist of literal characters and regular expression substitutions. Para saber mais, confira Substituições.For more information, see Substitutions.