Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

MatchEvaluator Delegate

The delegate that is called each time a regular expression match is found during a Replace operation.

[Visual Basic]
<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] In JScript, you can use the delegates in the .NET Framework, but you cannot define your own.

Parameters [Visual Basic, C#, C++]

The declaration of your callback method must have the same parameters as the MatchEvaluator delegate declaration.

match
The Match resulting from a single regular expression match during a Replace.

Remarks

You can use MatchEvaluator to perform custom verifications or operations at each Replace operation. For each match, your MatchEvaluator is called. Its return value is used as the replacement for the match passed in, and it can do any arbitrary processing to generate the replacement.

Example

[Visual Basic, C#, C++] The following example uses MatchEvaluator to replace every matched group of characters with the number of the match occurrence.

[Visual Basic] 
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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Text.RegularExpressions

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System (in System.dll)

See Also

System.Text.RegularExpressions Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.