08Dc91wk
08Dc91wk

Reputation: 4318

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

I ran a massive SQL query that I don't want to run again and saved the results as a csv. I am doing some processing in a C# console app that adds each record to a storage table. Unfortunately, I messed up and did not remove ','s from the results, and there are some objects serialized in JSON in this data which contain ','.

I am already looping through all this data, and so that my columns correctly line up I would just like to temporarily convert ',' to say ';', but only if it is between curly braces in the string. For example:

ID,InputModel,Type
1,{"address":"11th street"},true
2,{"address":"11th street, new york"},true

My code is as follows:

for (int j = 0; j < allLines.Length; j++)
{                    
    string line = allLines[j];
    // would like to replace ',' with ';' 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

Upvotes: 2

Views: 220

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

You can match a comma inside curly braces using the following regex:

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

You can replace it with a 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);
...
for (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

Upvotes: 4

Related Questions