c# - Regex replacement in strings -


i have string pairs :

s_1 : "he graduated in 1994 32 courses"  s_2 : "i graduated in 0000 00 courses" 

what want modify s_2, such 0000 gets changed 1994 , 00 32.

modified_s_2 : "i graduated in 1994 32 courses" 

basically, 0000...n_times...0 tells it's going matched string number n digits in s_1.

i can implement looping. looking efficient implementation. think regex implementation easy this.

note : there can n numbers in strings, , each number can have number of digits.

i think mean this:

var s_1 = "he graduated in 1994 32 courses"; var s_2 = "i graduated in 0000 00 courses 0000";  //// i'll find combination of '0's replaced var regexes =     regex.matches(s_2, @"\b0+\b")         .oftype<match>()         .select(c => new { c.value, reg = new regex(c.value.replace("0", @"\d")) })         .tolist();  //// replace each '0's combination first match  var curs1 = s_1; foreach (var regex in regexes) {     var s1value = regex.reg.match(curs1).value;     curs1 = regex.reg.replace(curs1, "", 1);     //// remove first match of s_1 don't matched again     s_2 = new regex(regex.value).replace(s_2, s1value, 1); } 

[c# demo]

a test cases can be:

var s_1 = "he graduated in 1994 32 courses 254 1998"; var s_2 = "i graduated in 0000 00 courses 000 0000"; 

that result be:

i graduated in 1994 32 courses 254 1998 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -