Replacing Regex matches using lambda expression

前端 未结 2 1217
一生所求
一生所求 2020-12-11 11:53

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(tex         


        
相关标签:
2条回答
  • 2020-12-11 12:17

    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

    0 讨论(0)
  • 2020-12-11 12:38

    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.

    0 讨论(0)
提交回复
热议问题