c# - How to replace Match value with new value in Regex -


i have string, this:

string str = "id=1,id=2,id=5,id=22"; 

then, apply regex on string, in order identifiers :

var idmatchcollection = regex.matches(str);  foreach(match match in idmatchcollection) {    var newvalue = somefunction(match.tostring());    // want replace newvalue match have in foreach newvalue. reflect in sting str. } 

so, final output should :

str = "id=234,id=576,id=5767,id=756765" 

(234,567,5767,756765) values got function in foreach loop for(1,2,5,22)

you want use regex.replace(string, matchevaluator) method calls callback function on every match.

here's sample msdn:

using system; using system.text.regularexpressions;  class regexsample {     static string captext(match m)     {         // matched string.          string x = m.tostring();         // if first char lower case...          if (char.islower(x[0]))         {             // capitalize it.              return char.toupper(x[0]) + x.substring(1, x.length - 1);         }         return x;     }      static void main()     {         string text = "four score , 7 years ago";          system.console.writeline("text=[" + text + "]");          regex rx = new regex(@"\w+");          string result = rx.replace(text, new matchevaluator(regexsample.captext));          system.console.writeline("result=[" + result + "]");     } } 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -