Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, is there any way to tell how many replacements Regex.Replace made? [duplicate]

Tags:

c#

.net

regex

Possible Duplicate:
Count regex replaces (C#)

Is there any way to tell how many replacements a Regex.Replace(...) call makes?

like image 480
Matt Avatar asked Feb 19 '23 03:02

Matt


1 Answers

You could use the Regex.Replace Method (String, String, MatchEvaluator) overload and establish a count with your custom MatchEvaluator.

You can always do this like so:

int count = 0;
string newStr = Regex.Replace(origStr, regexStr, m => { count++; return "replacement"; });
like image 60
Rudi Visser Avatar answered Feb 20 '23 19:02

Rudi Visser