Assuming the input is as simple as your examples (i.e. not complete CSV data), this should do it:
string input = @"a,b,c,""d,d,d"",e,f,""g,g"",h";
Console.WriteLine(input);
string result = Regex.Replace(input,
@",(?=[^""]*""(?:[^""]*""[^""]*"")*[^""]*$)",
String.Empty);
Console.WriteLine(result);
conclusion:
a, b, c, "d, d, d", e, f, "g, g", h
a, b, c, "ddd", e, f, "gg", h
A regular expression matches any comma followed by an odd number of quotes.
EDIT: if fields instead of apostrophes ( '
) are specified instead of quotes ( "
), then the technique will be exactly the same, except that you do not need to hide quotes:
string input = @"a,b,c,'d,d,d',e,f,'g,g',h";
Console.WriteLine(input);
string result = Regex.Replace(input,
@",(?=[^']*'(?:[^']*'[^']*')*[^']*$)",
String.Empty);
Console.WriteLine(result);
If some fields were quoted with apostrophes and others with quotes, a different approach would be required.
EDIT: , , , , ( ):
@",(?=[^']*'(?:[^']*'[^']*')*[^']*$|[^""]*""(?:[^""]*""[^""]*"")*[^""]*$)"
, , 'a,a',"b,b"
. , , '9" Nails'
(sic) "Kelly Heroes"
. CSV ( ), , .: D