技术问询:如何移除双引号内部的逗号?
移除CSV字段内的逗号(保留分隔逗号)
问题场景
原始CSV行示例:
12,"text, is","another 25,000",23,"hello"期望处理后结果:
12,"text is","another 25000",23,"hello"需求:仅移除双引号包裹的字段内部的逗号,保留字段之间的分隔逗号。
尝试过的无效代码
public override void Input0_ProcessInputRow(Input0Buffer Row) { //String Pattern = "(?<=\".*),(?=.*\")"; //String Result = Regex.Replace(Row.Column0.Trim(), Pattern, "@"); //String Result = Regex.Replace(Row.Column0, @"[""]", "", RegexOptions.None); //String Result = Row.Column0.Replace("0,", "0|").Replace("1,", "1|").Replace("2,", "2|").Replace("3,", "3|").Replace("4,", "4|").Replace("5,", "5|").Replace("6,", "6|").Replace("7,", "7|").Replace("8,", "8|").Replace("9,", "9|"); //String Result = Row.Column0.Replace("\",", "|").Replace(",\"", "|"); //String Result = Row.Column0.Replace(",", "").Replace("\"", ""); }
可行解决方案
使用正则匹配所有双引号包裹的字段,再对每个字段单独移除内部逗号:
public override void Input0_ProcessInputRow(Input0Buffer Row) { string processedRow = Regex.Replace(Row.Column0, @"(""[^""]+"")", match => { // 移除当前匹配字段内的所有逗号 return match.Value.Replace(",", ""); }); // 将处理后的结果赋值到输出列(根据你的实际需求调整) // Row.OutputColumn = processedRow; }
逻辑说明
- 正则表达式
@"(""[^""]+"")":精准匹配每个被双引号包裹的字段,不会误匹配字段间的分隔逗号。 - 替换委托:对每个匹配到的字段(如
"text, is"),直接移除内部所有逗号后返回,确保只修改字段内的内容。
内容的提问来源于stack exchange,提问作者Henry
相关产品推荐
相关产品推荐

