c# - String function - replace instances of a character if they are between two other characters -


i ran massive sql query don't want run again , saved results csv. doing processing in c# console app adds each record storage table. unfortunately, messed , did not remove ','s results, , there objects serialized in json in data contain ','.

i looping through data, , columns correctly line temporarily convert ',' ';', if between curly braces in string. example:

id,inputmodel,type 1,{"address":"11th street"},true 2,{"address":"11th street, new york"},true 

my code follows:

for (int j = 0; j < alllines.length; j++) {                         string line = alllines[j];     // replace ',' ';' if between curly braces     string[] data = line.split(',');     myinsertmethod(data); } 

desired result:

id,inputmodel,type 1,{"address":"11th street"},true 2,{"address":"11th street; new york"},true 

you can match comma inside curly braces using following regex:

(?<=\{[^}]*),(?=[^}]*\}) 

you can replace semi-colon:

var rgx = new regex(@"(?<=\{[^}]*),(?=[^}]*\})"); var result = rgx.replace("{word, word}, {word, word}", ";"); 

result: {word; word}, {word; word}

your code:

private static readonly regex rgx = new regex(@"(?<=\{[^}]*),(?=[^}]*\})", regexoptions.compiled); ... (int j = 0; j < alllines.length; j++) {                         var line = alllines[j];     var data = rgx.replace(line, ";").split(',');     myinsertmethod(data); } 

tested in expresso:

enter image description here


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -