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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DotNetNewbie

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

    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(o ldText, 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(o ldText, myRegex,
    String.Format(@ "<a href="$1">{0}</a>",
    MyFunction("$1" )),
    regexOptions);
  • Marc Gravell

    #2
    Re: How to modify the value of a named capture before doing areplace?

    Try:

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

    Marc

    Comment

    • DotNetNewbie

      #3
      Re: How to modify the value of a named capture before doing areplace?

      On Apr 10, 5:26 pm, Marc Gravell <marc.grav...@g mail.comwrote:
      Try:
      >
      string text = Regex.Replace(o ldText, myRegex, delegate (Match
      match) {
      string capture1 = match.Captures[0].Value;
      return string.Format(@ "<a href=""{0}"">{1 }</a>",
      capture1, MyFunction(capt ure1));
      }, regexOptions);
      >
      Marc
      Marc, it didn't compile for some reason (i.e. there is not method that
      takes in a delegate?)

      Comment

      Working...