用空格分隔字符串,但忽略引号中的空格 [英] Splitting string by spaces but ignore spaces inside a quote

查看:791
本文介绍了用空格分隔字符串,但忽略引号中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的字符串,它有一个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.

编辑:没有看到您使用的是C#。 这里 更多 链接

Didn't see you were using C#. Here are more links.

这篇关于用空格分隔字符串,但忽略引号中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆