Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing Regex matches using lambda expression

I'm looking for a simple regex find and replace solution were I can just provide a lambda expression for replacing each matches. E.g:

regex.MatchReplace(text, match => "replacement string");

This way I can create my own logic for generating the replacement string which may involve invoking various methods etc. i.e. things you can't do with substitution patterns. Anyone know how I can accomplish this?

like image 891
Pking Avatar asked Jul 29 '26 15:07

Pking


1 Answers

Regex already has one. For ex,

string input="abc123def";
var output = Regex.Replace(input, @"\d", m=>(m.Value[0]-'0'+ 5).ToString());
Console.WriteLine(output);

OUTPUT: abc678def

like image 158
EZI Avatar answered Aug 03 '26 10:08

EZI



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!