MatchEvaluator デリゲート
アセンブリ: System (system.dll 内)

/** @delegate */ /** @attribute SerializableAttribute() */ public delegate String MatchEvaluator ( Match match )
戻り値
MatchEvaluator デリゲートが表すメソッドによって返された文字列。

MatchEvaluator デリゲート メソッドを使用すると、Regex.Replace(String,MatchEvaluator) などの置換メソッドで見つかった各一致について、カスタム検証や文字列操作を実行できます。Replace メソッドは一致した文字列ごとに MatchEvaluator デリゲート メソッドを呼び出します。このとき、デリゲート メソッドには、一致を表す Match オブジェクトが引数として渡されます。デリゲート メソッドは必要な処理を実行し、Replace メソッドが一致した文字列を置き換えるための文字列を返します。

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- MatchEvaluator デリゲートのページへのリンク