MatchEvaluator デリゲート

Replace 操作中に、正規表現と一致する対象が見つかるたびに呼び出されるデリゲート。

<Serializable>
Public Delegate Function Sub MatchEvaluator( _   ByVal match As Match _) As String
[C#]
[Serializable]
public delegate string MatchEvaluator(   Match match);
[C++]
[Serializable]
public __gc __delegate String* MatchEvaluator(   Match* match);

[JScript] JScript では、.NET Framework のデリゲートを利用することができます。ただし、独自に定義することはできません。

パラメータ [Visual Basic, C#, C++]

コールバック メソッドの宣言のパラメータは、MatchEvaluator デリゲートの宣言と同じでなければなりません。

  • match
    Replace 操作中に単一の正規表現と一致した結果の Match

解説

MatchEvaluator を使用すると、 Replace 操作が行われるたびにカスタムの検証および操作を実行できます。一致する対象が見つかるたびに、 MatchEvaluator が呼び出されます。その戻り値は、渡された一致を置換する値として使用されます。置換する値は任意の処理方法で生成できます。

使用例

[Visual Basic, C#, C++] 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

[C#] 
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;
   }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Text::RegularExpressions;

__gc 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, * sRegex;

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

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

    Regex* r = new Regex(sRegex);

    // Assign the replace method to the MatchEvaluator delegate.
    MatchEvaluator* myEvaluator = new MatchEvaluator(0, &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);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Text.RegularExpressions

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: System (System.dll 内)

参照

System.Text.RegularExpressions 名前空間