Use the Regex.Replace(String, MatchEvaluator)
overload.
static void Main()
{
string input = "[MyAppTerms.TermName1]. [MyAppTerms.TermName2]. 1- [MyAppTerms.TermNameX] 2";
Regex regex = new Regex(@"\[MyAppTerms\.([^\]]+)\]");
string output = regex.Replace(input, new MatchEvaluator(RegexReadTerm));
Console.WriteLine(output);
}
static string RegexReadTerm(Match m)
{
// The term name is captured in the first group
return ReadTerm(m.Groups[1].Value);
}
The pattern \[MyAppTerms\.([^\]]+)\]
matches your [MyAppTerms.XXX]
tags and captures the XXX
in a capture group. This group is then retrieved in your MatchEvaluator
delegate and passed to your actual ReadTerm
method.
It's even better with lambda expressions (since C# 3.0):
static void Main()
{
string input = "[MyAppTerms.TermName1]. [MyAppTerms.TermName2]. 1- [MyAppTerms.TermNameX] 2";
Regex regex = new Regex(@"\[MyAppTerms\.([^\]]+)\]");
string output = regex.Replace(input, m => ReadTerm(m.Groups[1].Value));
Console.WriteLine(output);
}
Here, you define the evaluator straight inside the code which uses it (keeping logically connected pieces of code together) while the compiler takes care of constructing that MatchEvaluator
delegate.