Match.Result(String) 메서드

정의

지정된 바꾸기 패턴의 확장을 반환합니다.

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

사용할 바꾸기 패턴입니다.

반환

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)

정규식 패턴 --(.+?)-- 는 다음 테이블과 같이 해석됩니다.

무늬 설명
-- 두 개의 하이픈을 일치합니다.
(.+?) 문자를 한 번 이상 일치하지만 가능한 한 적은 수만큼 일치합니다. 이 그룹은 첫 번째 캡처링 그룹입니다.
-- 두 개의 하이픈을 일치합니다.

정규식 패턴 --(.+?)-- 은 지연 수량자를 +?사용합니다. 대신 greedy 수량자를 + 사용한 경우 정규식 엔진은 입력 문자열에서 단일 일치 항목만 찾습니다.

대체 문자열 ($1) 은 일치 항목을 괄호로 묶은 첫 번째 캡처된 그룹으로 바꿉니다.

설명

메서드는 입력 문자열의 Regex.Replace 모든 일치 항목을 지정된 대체 패턴으로 바꾸는 반면, 메서드는 Result 단일 일치 항목을 지정된 대체 패턴으로 바꿉니다. 개별 일치 항목에서 작동하므로 메서드를 호출 Result 하기 전에 일치하는 문자열에서 처리를 수행할 수도 있습니다.

replacement 매개 변수는 표준 정규식 대체 패턴입니다. 리터럴 문자와 정규식 대체로 구성 될 수 있습니다. 자세한 내용은 대체를 참조하세요.

적용 대상

추가 정보