MatchEvaluator 대리자

정의

Replace 메서드 작업을 하는 동안 정규식을 사용하여 일치 항목을 찾을 때마다 호출되는 메서드를 나타냅니다.

public delegate System::String ^ MatchEvaluator(Match ^ match);
public delegate string MatchEvaluator(Match match);
[System.Serializable]
public delegate string MatchEvaluator(Match match);
type MatchEvaluator = delegate of Match -> string
[<System.Serializable>]
type MatchEvaluator = delegate of Match -> string
Public Delegate Function MatchEvaluator(match As Match) As String 

매개 변수

match
Match

Match 메서드 작업을 하는 동안 단일 정규식 일치 항목을 나타내는 Replace 개체입니다.

반환 값

String

MatchEvaluator 대리자가 나타내는 메서드에서 반환된 문자열입니다.

특성

예제

다음 코드 예제에서는 대리자를 MatchEvaluator 사용하여 일치하는 모든 문자 그룹을 일치 항목 수로 바꿉니다.

#using <System.dll>

using namespace System;
using namespace System::Text::RegularExpressions;
ref class MyClass
{
public:
   static int i = 0;
   static String^ ReplaceCC( Match^ m )
   {
      
      // Replace each Regex cc match with the number of the occurrence.
      i++;
      return i.ToString();
   }

};

int main()
{
   String^ sInput;
   String^ sRegex;
   
   // The string to search.
   sInput = "aabbccddeeffcccgghhcccciijjcccckkcc";
   
   // A very simple regular expression.
   sRegex = "cc";
   Regex^ r = gcnew Regex( sRegex );
   
   // Assign the replace method to the MatchEvaluator delegate.
   MatchEvaluator^ myEvaluator = gcnew MatchEvaluator( &MyClass::ReplaceCC );
   
   // Write out the original string.
   Console::WriteLine( sInput );
   
   // Replace matched characters using the delegate method.
   sInput = r->Replace( sInput, myEvaluator );
   
   // Write out the modified string.
   Console::WriteLine( sInput );
}
// The example displays the following output:
//       aabbccddeeffcccgghhcccciijjcccckkcc
//       aabb11ddeeff22cgghh3344iijj5566kk77
using System;
using System.Text.RegularExpressions;

class MyClass
{
   static void Main(string[] args)
   {
      string sInput, sRegex;

      // The string to search.
      sInput = "aabbccddeeffcccgghhcccciijjcccckkcc";

      // A very simple regular expression.
      sRegex = "cc";

      Regex r = new Regex(sRegex);
        
      MyClass c = new MyClass();

      // Assign the replace method to the MatchEvaluator delegate.
      MatchEvaluator myEvaluator = new MatchEvaluator(c.ReplaceCC);
        
      // Write out the original string.
      Console.WriteLine(sInput);

      // Replace matched characters using the delegate method.
      sInput = r.Replace(sInput, myEvaluator);
      
      // Write out the modified string.
      Console.WriteLine(sInput);
   }

   public string ReplaceCC(Match m)
   // Replace each Regex cc match with the number of the occurrence.
   {
      i++;
      return i.ToString() + i.ToString();		
   }
   public static int i=0;
}
// The example displays the following output:
//       aabbccddeeffcccgghhcccciijjcccckkcc
//       aabb11ddeeff22cgghh3344iijj5566kk77
Imports System.Text.RegularExpressions

Module Module1
   Public Sub Main()
      Dim sInput, sRegex As String

      ' The string to search.
      sInput = "aabbccddeeffcccgghhcccciijjcccckkcc"

      ' A very simple regular expression.
      sRegex = "cc"

      Dim r As Regex = New Regex(sRegex)

      ' Assign the replace method to the MatchEvaluator delegate.
      Dim myEvaluator As MatchEvaluator = New MatchEvaluator(AddressOf ReplaceCC)

      ' Write out the original string.
      Console.WriteLine(sInput)
      ' Replace matched characters using the delegate method.
      sInput = r.Replace(sInput, myEvaluator)
      ' Write out the modified string.
      Console.WriteLine(sInput)
   End Sub

   Public Function ReplaceCC(ByVal m As Match) As String
      ' Replace each Regex match with the number of the match occurrence.
      static i as integer
   
      i = i + 1
      Return i.ToString() & i.ToString()
   End Function
End Module
' The example displays the following output:
'       aabbccddeeffcccgghhcccciijjcccckkcc
'       aabb11ddeeff22cgghh3344iijj5566kk77

설명

대리자 메서드를 MatchEvaluator 사용하여 대체 Regex.Replace(String, MatchEvaluator)메서드에서 찾은 각 일치 항목에 대해 사용자 지정 확인 또는 조작 작업을 수행할 수 있습니다. 일치하는 각 문자열에 대해 메서드는 Replace 일치 항목을 MatchEvaluator 나타내는 개체를 사용하여 Match 대리자 메서드를 호출합니다. 대리자 메서드는 원하는 모든 처리를 수행하고 메서드가 Replace 일치하는 문자열을 대체하는 문자열을 반환합니다.

확장 메서드

GetMethodInfo(Delegate)

지정된 대리자가 나타내는 메서드를 나타내는 개체를 가져옵니다.

적용 대상

추가 정보