MatchEvaluator 代理人
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示每次在 Replace 方法作業期間找到規則運算式 (Regular Expression) 相符項目時都會呼叫的方法。
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
參數
傳回值
字串,由 MatchEvaluator 委派 (Delegate) 所表示的方法傳回。
- 屬性
範例
下列程式碼範例會 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 使用 Match 代表相符專案的 物件來呼叫 MatchEvaluator 委派方法。 委派方法會執行您偏好的任何處理,並傳回方法取代相符字串的字串 Replace 。
擴充方法
GetMethodInfo(Delegate) |
取得表示特定委派所代表之方法的物件。 |