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

MatchEvaluator Delegate

Represents the method that is called each time a regular expression match is found during a Replace method operation.

Namespace: System.Text.RegularExpressions
Assembly: System (in system.dll)

[SerializableAttribute] 
public delegate string MatchEvaluator (
	Match match
)
/** @delegate */
/** @attribute SerializableAttribute() */ 
public delegate String MatchEvaluator (
	Match match
)
JScript supports the use of delegates, but not the declaration of new ones.

Parameters

match

The Match object that represents a single regular expression match during a Replace method operation.

Return Value

A string returned by the method that is represented by the MatchEvaluator delegate.

You can use a MatchEvaluator delegate method to perform a custom verification or manipulation operation for each match found by a replacement method such as Regex.Replace(String,MatchEvaluator). For each matched string, the Replace method calls the MatchEvaluator delegate method with a Match object that represents the match. The delegate method performs whatever processing you prefer and returns a string that the Replace method substitutes for the matched string.

The following code example uses the MatchEvaluator delegate to replace every matched group of characters with the number of the match occurrence.

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;
   }
}

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.