Basically I am dealing with CSV file and reading it line by line in C#. I have a string input(a line) and trying to find a Regex pattern and replace it using another Regex pattern but result is not what I expect.
var input = "\"efgh ,ijkl123,\",abcd , \"efgh ,ijkl123,\",mnop456 \"efgh ,ijkl123,\"";
In output I need to replace internal commas between double quotations with semicolon where those double quotation are between commas themselves.
between double quotation and external comma (comma outside of pair of double quotes) it can be only white space.
So I expect output to be: "efgh ;ijkl123,",abcd , "efgh ;ijkl123,",mnop456 "efgh ,ijkl123,"
my code:
var pattern = @".*,\s*""(.*,+.*)+""\s*,.*";
var replacePattern = @".*,\s*""(.*;+.*)+""\s*,.*";
if (Regex.IsMatch(input, pattern))
{
var output = Regex.Replace(input, pattern, replacePattern);
}
but running my code, output is: .,\s"(.;+.)+"\s*,.* which is replacePattern.
EDIT more input sample and output as expected:
input
abcd , "efgh ,ijkl123,",mnop456output
abcd , "efgh ;ijkl123;",mnop456input
"efgh ,ijkl123,",abcd , "efgh ,ijkl123,",mnop456 "efgh ,ijkl123,"output
"efgh ;ijkl123;",abcd , "efgh ;ijkl123;",mnop456 "efgh ,ijkl123,"input
,"efgh ,ijkl123,",abcd" , "efgh ijkl123,",mnop456 "efgh ,ijkl123,","efgh ,ijkl123,"mnop456output
,"efgh ;ijkl123;",abcd" , "efgh ijkl123;",mnop456 "efgh ,ijkl123,","efgh ,ijkl123,"mnop456input
,"efgh" ,ijkl123,",abcd" , "efgh ijkl123,",mnop456 "efgh ,ijkl123,","efgh ,ijkl123,"mnop456output
,"efgh" ,ijkl123,";abcd" , "efgh ijkl123;",mnop456 "efgh ,ijkl123,","efgh ,ijkl123,"mnop456input
efgh ,ijkl123,",abcd , "efgh ,ijkl123,",mnop456 "efgh ,ijkl123,"output
efgh ,ijkl123,",abcd , "efgh ;ijkl123;",mnop456 "efgh ,ijkl123,"
,coma with;semi colon if it's between double quotes?