How to modify the value of a named capture before doing a replace?

  • Thread starter Thread starter DotNetNewbie
  • Start date Start date
D

DotNetNewbie

Hello,

I have a regex and I want to modify the value of a named capture
before doing the regex.Replace.

So something like:

string text = Regex.Replace(oldText, myRegex, @"<a href="$1">$1</a>",
regexOptions);


Now I want to modify the value of the second $1 just in case it is too
long to display.

This doesn't work, but this is what I want to do somehow!

string text = Regex.Replace(oldText, myRegex,
String.Format(@"<a href="$1">{0}</a>",
MyFunction("$1")),
regexOptions);
 
Try:

string text = Regex.Replace(oldText, myRegex, delegate (Match
match) {
string capture1 = match.Captures[0].Value;
return string.Format(@"<a href=""{0}"">{1}</a>",
capture1, MyFunction(capture1));
}, regexOptions);

Marc
 
Try:

string text = Regex.Replace(oldText, myRegex, delegate (Match
match) {
string capture1 = match.Captures[0].Value;
return string.Format(@"<a href=""{0}"">{1}</a>",
capture1, MyFunction(capture1));
}, regexOptions);

Marc

Marc, it didn't compile for some reason (i.e. there is not method that
takes in a delegate?)
 
Back
Top