public delegate string MatchEvaluator(Match match);
Examples
using System;
using System.Text.RegularExpressions;
class MyClass
{
static void Main(string[] args)
{
string sInput, pattern;
// The string to search.
sInput = "aabbccddeeffcccgghhcccciijjcccckkcc";
// A very simple regular expression.
pattern = "cc";
Regex r = new Regex(pattern);
// Assign the replace method to the MatchEvaluator delegate.
MatchEvaluator myEvaluator = new MatchEvaluator(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);
}
// Replace each Regex cc match with the number of the occurrence.
public static string ReplaceCC(Match m)
{
i++;
return i.ToString() + i.ToString();
}
public static int i=0;
}
// The example displays the following output:
// aabbccddeeffcccgghhcccciijjcccckkcc
// aabb11ddeeff22cgghh3344iijj5566kk77
没有评论:
发表评论