通过逗号分隔字符串,忽略引号中的任何标点符号(包括“,”) [英] Split string by commas ignoring any punctuation marks (including ',') in quotation marks
本文介绍了通过逗号分隔字符串,忽略引号中的任何标点符号(包括“,”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何用逗号分隔( 用双引号(而不除去引号)以及其他可能的标点符号)(用文本框分隔字符串)标记(例如'。'';''-')?
How can I split string (from a textbox) by commas excluding those in double quotation marks (without getting rid of the quotation marks), along with other possible punctuation marks (e.g. ' . ' ' ; ' ' - ')?
例如如果有人在文本框中输入以下内容:
E.g. If someone entered the following into the textbox:
apple, orange, "baboons, cows", rainbow, "unicorns, gummy bears"
如何将上述字符串拆分为以下字符串(例如,分为列表)?
How can I split the above string into the following (say, into a List)?
apple
orange
"baboons, cows"
rainbow
"Unicorns, gummy bears..."
谢谢您的帮助!
推荐答案
您可以尝试下面的正则表达式正则表达式,
You could try the below regex which uses positive lookahead,
string value = @"apple, orange, ""baboons, cows"", rainbow, ""unicorns, gummy bears""";
string[] lines = Regex.Split(value, @", (?=(?:""[^""]*?(?: [^""]*)*))|, (?=[^"",]+(?:,|$))");
foreach (string line in lines) {
Console.WriteLine(line);
}
输出:
apple
orange
"baboons, cows"
rainbow
"unicorns, gummy bears"
这篇关于通过逗号分隔字符串,忽略引号中的任何标点符号(包括“,”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文