I'm looking for a simple regex find and replace solution were I can just provide a lambda expression for replacing each matches. Eg:
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. ie things you can't do with substitution patterns. Anyone know how I can accomplish this?
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
Please have a look at the following:
https://msdn.microsoft.com/en-GB/library/bb383977.aspx
You can define an extension method for the RegEx class which will allow you to specify an Action<>
as an argument.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.