如何使用 C# 和正则表达式删除引号 (") 内的所有逗号

How to remove all commas that are inside quotes (") with C# and regex

如何使用 C# 构建正则表达式以删除引号 (") 内的所有逗号,然后用 @ 替换它们?

例子:

像这样的初始字符串= (value 1,value 2,"value3,value4,value5",value 6)

期望这样的字符串= (value 1,value 2,"value3@value4@value5", value 6)

string input = "= (value 1,value 2,\"value3,value4,value5\",value 6)";
string pattern = "(?<=\".*),(?=.*\")";
string result = Regex.Replace(input, pattern, "@");

您可以使用

string input = "(value 1,value 2,\"value3,value4,value5\",value 6)";
var regex = new Regex("\\"(.*?)\\"");
var output = regex.Replace(input, m => m.Value.Replace(',','@'));

下面提到的正则表达式模式可以识别双引号内的数据,即使在多层次中也是如此 Regex pattern: ([\"].*[\"])

        List<string> input = new List<string>();
        input.Add("= (value 1,value 2,\"value3,value4,value5\",value 6)");
        input.Add("\"(value 1,value 2,\"value 3, value 4\",value 5,value 6)\"");
        var regex = new Regex("([\"].*[\"])");

        List<string> output = input.Select(data => regex.Replace(data, m=> m.Value.Replace(',','@'))).ToList();

        foreach(string dat in output)
            Console.WriteLine(dat);