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 =>
	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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.
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

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.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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.
käµfm³d 👽Commented:
What is wrong with the code you originally posted?
rye004Author Commented:
This was the best answer.
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

From novice to tech pro — start learning today.