正则表达式以逗号分割,除非引用

除了用双引号括起来之外,在逗号(,)上拆分的正则表达式是什么? 例如:

max,emily,john = ["max", "emily", "john"] 

 max,"emily,kate",john = ["max", "emily,kate", "john"] 

希望在C#中使用: Regex.Split(string, "PATTERN-HERE");

谢谢。

像这样的情况通常需要除正则表达式之外的其他东西。 它们很漂亮,但处理这种事情的模式比它们有用的更复杂。

您可以尝试这样的事情:

 public static IEnumerable SplitCSV(string csvString) { var sb = new StringBuilder(); bool quoted = false; foreach (char c in csvString) { if (quoted) { if (c == '"') quoted = false; else sb.Append(c); } else { if (c == '"') { quoted = true; } else if (c == ',') { yield return sb.ToString(); sb.Length = 0; } else { sb.Append(c); } } } if (quoted) throw new ArgumentException("csvString", "Unterminated quotation mark."); yield return sb.ToString(); } 

可能需要一些调整才能完全遵循CSV规范,但基本逻辑是合理的。

对于CSV解析器来说,这是一个明确的案例,因此您应该使用.NET自己的CSV解析function或cdhowie的解决方案。

纯粹是为了您的信息,而不是一个可行的解决方案,下面是使用正则表达式与Regex.Split()进行的Regex.Split()

你可以使用正则表达式( 请不要!

 (?<=^(?:[^"]*"[^"]*")*[^"]*) # assert that there is an even number of quotes before... \s*,\s* # the comma to be split on... (?=(?:[^"]*"[^"]*")*[^"]*$) # as well as after the comma. 

如果您引用的字符串从不包含转义引号, 并且您不介意引号本身成为匹配的一部分。

这非常低效,读取和调试很痛苦,只能在.NET中运行,并且在转义引号上失败(至少如果你没有使用""来逃避单引号)。 当然可以修改正则表达式来处理它,但那时它将是完全可怕的。

可能有点晚,但我希望我可以帮助别人

  String[] cols = Regex.Split("max, emily, john", @"\s*,\s*"); foreach ( String s in cols ) { Console.WriteLine(s); } 

贾斯汀,复活这个问题,因为它有一个简单的正则表达式解决方案,没有提到。 除了情况s1,s2,s3等之外,这种情况听起来直接来自匹配(或替换)模式 。

这是我们简单的正则表达式:

 "[^"]*"|(,) 

交替的左侧匹配完整的"quoted strings"标签。 我们将忽略这些匹配。 右侧匹配并捕获第1组的逗号,我们知道它们是正确的逗号,因为它们与左侧的表达式不匹配。 我们用SplitHere替换这些逗号,然后我们拆分SplitHere

该程序显示了如何使用正则表达式(请参阅在线演示底部的结果):

 using System; using System.Text.RegularExpressions; using System.Collections.Specialized; class Program { static void Main() { string s1 = @"max,""emily,kate"",john"; var myRegex = new Regex(@"""[^""]*""|(,)"); string replaced = myRegex.Replace(s1, delegate(Match m) { if (m.Groups[1].Value == "") return m.Value; else return "SplitHere"; }); string[] splits = Regex.Split(replaced,"SplitHere"); foreach (string split in splits) Console.WriteLine(split); Console.WriteLine("\nPress Any Key to Exit."); Console.ReadKey(); } // END Main } // END Program 

参考

如何匹配(或替换)模式除了情况s1,s2,s3 ……