Can I determine how many changes Regex.Replace has made in C#?

I am trying to use the code below to make changes and tell me the number of changes that have been made.  

MatchEvaluator m;
int matchCount = 0;
string outputString;

currentFileText = Regex.Replace(currentFileText, currentRedactionRule.inputString, currentRedactionRule.patternString, m =>
    {
	matchCount++;
	return outputString;
    });

Open in new window


I would prefer not to use a MatchCollection and a foreeach outside of this call.  Performance is important to me and I would rather get this value if it already exists.
Hopefully this makes sense.  Any help would be greatly appreciated.
rye004Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
Not directly.

But at the detriment of a small price in performance (maybe a big price if you expect a lot of replacements), you could do the job in a while loop, with the overload of Replace that limits the number of replacements to perform. Simply set that parameter to 1, loop for as long as the string changes (compare the returned value with the original value) and count the number of turns in the loop.

If the replacement is a straight one, you could also use string.IndexOf in a loop to count the number of instances of the string to be replaced before you do the job.

Or use string.DistincOf to look for that string and get the count on the returned array.
0
rye004Author Commented:
Thank you for your response.

I think the following is going to be the easiest:

                                        //See if there any occurences
                                        MatchCollection currentMatchCollection2 = Regex.Matches(currentFileText, currentRedactionRule.inputString, RegexOptions.Singleline);

                                        if(currentMatchCollection2.Count > 0)
                                            currentFileText = Regex.Replace(currentFileText, currentRedactionRule.inputString, currentRedactionRule.patternString);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jacques Bourgeois (James Burger)PresidentCommented:
I did not know about that one. I am the one who learned something here.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

rye004Author Commented:
It works, but I have to evaluate the text twice when there is a match.  I would prefer a way to only call the Regex class once.  I will leave this question open for a bit to see if anyone else has any ideas.

Thanks again for your help.
0
käµfm³d 👽Commented:
What is wrong with the code you originally posted?
0
rye004Author Commented:
This was the best answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.