Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
190 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...