5

I am trying to replace a single (last or next-to-last) match in a string. I have my Regular Expression already and it works, but it replaces ALL items, then I have to run back through and replace a single item.

Regex.Replace(BaseString, MatchString, ReplacementString)

I want to use MatchEvaluator but can't figure out how.

Any help?

1
  • Depending on your usage, you should consider creating a Regex instance.
    – SLaks
    Dec 30, 2010 at 22:50

3 Answers 3

13

MatchEvaluator is simply a delegate. You pass in your function.

In your case, use something like:

Regex.Replace(BaseString, MatchString, delegate(Match match)
                    {
                        bool last = match.NextMatch().Index == 0;

                        if (last)
                            return match.Value;
                        else
                            return ReplacementString;
                    }, RegexOptions.Compiled);

This code skips the last match. You could also check for next-to-last match in a similar manner.

Also see this question for more information on how to use MatchEvaluator: How does MatchEvaluator in Regex.Replace work?

4

This is to replace the last occurance in the string:

String testString = "It is a very good day isn't it or is it.Tell me?";
Console.WriteLine(Regex.Replace(testString, 
                  "(.*)(it)(.*)$", 
                  "$1THAT$3", 
                   RegexOptions.IgnoreCase));

If you can provide when the next to last should be replace, I can edit the answer to include that.

5
  • I'm not sure what you mean. The next to last should be the second-to-last match. If you have haystack string: "Trying to find a particular string." - then the "next to last" would be the "i" in "particular". Dec 30, 2010 at 23:35
  • I understood what next to last means, but confused when you say last or next-to-last should be replaced. Do you mean last two occurances should be replaced?
    – Chandu
    Dec 30, 2010 at 23:36
  • Fair enough. No, in "next to last" I just wanted the second to last to be replaced, but not the last. So, in the above Haystrick string: the "i" in "particular" but not the "i" in "string". Dec 30, 2010 at 23:38
  • So if the text occurs twice or more then next to last shd be replaced, else the last occurance..Will update the answer...
    – Chandu
    Dec 30, 2010 at 23:41
  • 1
    If I had known how to do "next to last" in regex, I would have done that, so it would be useful for using regex outside C# (where I can't use the MatchEvaluator delegate function). Dec 31, 2010 at 15:34
0

find in loop, save the last match position, then replace

2
  • Thanks, but I am looking for a non for-each option, I want to do it in a more elegant fashion. Your method would definitely work. Dec 30, 2010 at 22:51
  • i believe that regex just wasn't designed for loops Dec 31, 2010 at 0:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.