MatchEvaluator デリゲートとは? わかりやすく解説

MatchEvaluator デリゲート

Replace メソッド操作中に正規表現との一致が見つかるたびに呼び出されるメソッド表します

名前空間: System.Text.RegularExpressions
アセンブリ: System (system.dll 内)
構文構文

<SerializableAttribute> _
Public Delegate Function
 MatchEvaluator ( _
    match As Match _
) As String
Dim instance As New MatchEvaluator(AddressOf
 HandlerMethod)
[SerializableAttribute] 
public delegate string MatchEvaluator (
    Match match
)
[SerializableAttribute] 
public delegate String^ MatchEvaluator (
    Match^ match
)
/** @delegate */
/** @attribute SerializableAttribute() */ 
public delegate String MatchEvaluator (
    Match match
)
JScript では、デリゲート使用できますが、新規に宣言することはできません。

パラメータ

match

Replace メソッド操作中に1 つ正規表現見つかった一致を表す Match オブジェクト

戻り値
MatchEvaluator デリゲートが表すメソッドによって返され文字列

解説解説
使用例使用例

MatchEvaluator デリゲート使用して一致する文字グループが見つかるたびに、何番目の一致部分かを示す数値置換するコード例次に示します

Imports System
Imports System.Text.RegularExpressions

Namespace MyNameSpace
   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.
         Dim s As String
         static i as integer
      
         i = i + 1
         Return i.ToString() & i.ToString()
      End Function
   End Module
End Namespace
using System;
using System.Text.RegularExpressions;

namespace MyNameSpace
{
   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;
   }
}
#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 );
}

package MyNameSpace; 

import System.*;
import System.Text.RegularExpressions.*;

class MyClass
{
    public 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);
    } //main

    public String ReplaceCC(Match m)
    {
        // Replace each Regex cc match with the number of the occurrence.
        i++;
        return String.valueOf(i) + String.valueOf(i);
    } //ReplaceCC

    public static int i
 = 0;
} //MyClass
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
System.Text.RegularExpressions 名前空間
Replace



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「MatchEvaluator デリゲート」の関連用語

MatchEvaluator デリゲートのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



MatchEvaluator デリゲートのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS