用空格分隔字符串,但忽略引号中的空格 [英] Splitting string by spaces but ignore spaces inside a quote
问题描述
我有一个大的字符串,它有一个csv文件中的数据,但是当使用正则表达式如:
I have a big string which has got data from a csv file, however when using regular expressions such as:
Regex regex = new Regex(@"\w+|""[\w\s]*""");
它会拆分每个字母吗?没有空格foreach行,只在行的末尾 - 但不应该切割的行在双引号内有一个空格。
it splits every letter instead? there are no spaces foreach line, only at the end of the line - but shouldn't be cutting the line where there is a space inside double quotes.
示例:test1,test2,test3,test4,test5,test 6,test7(new
line)test8,test9等。
example: test1,test2,test3,test4,test5,"test 6",test7 (new line)test8,test9,etc.
有人可以指导我朝正确的方向吗?感谢
Can somebody guide me in the right direction? thanks
推荐答案
我建议您参考现有的解决方案比重新创建自己的解决方案(除非你要学习经验!)解析CSV比看起来更麻烦。
I recommend referring to an existing solution than reinventing your own (unless you're going for the learning experience!) Parsing CSV is trickier than it seems.
Didn't see you were using C#. Here are more links.
这篇关于用空格分隔字符串,但忽略引号中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!