正则表达式分割线(CSV文件)分割线、文件、正则表达式、CSV

2023-09-02 23:59:23 作者:欲望满身.

我不是在正则表达式好。可有一个人帮我写的正则表达式我?

I am not good in regex. Can some one help me out to write regex for me?

在阅读csv文件我可能有这样的价值。

I may have values like this while reading csv file.


"Artist,Name",Album,12-SCS
"val""u,e1",value2,value3

输出:


Artist,Name  
Album
12-SCS
Val"u,e1 
Value2 
Value3

更新: 我喜欢的想法使用OLEDB提供程序。我们确实有在网页文件上传控件,我读使用流读取器没有在文件系统上的实际文件保存文件的内容。有没有什么方法可以让我的用户OLEDB提供,因为我们需要在连接字符串,在我的情况下指定文件名我没有文件保存在文件系统中。

Update: I like idea using Oledb provider. We do have file upload control on the web page, that I read the content of the file using stream reader without actual saving file on the file system. Is there any way I can user Oledb provider because we need to specify the file name in connection string and in my case i don't have file saved on file system.

推荐答案

只是增加我的工作今天上午的解决方案。

Just adding the solution I worked on this morning.

var regex = new Regex("(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)");

foreach (Match m in regex.Matches("<-- input line -->"))
{
    var s = m.Value; 
}

正如你所看到的,你需要调用regex.Matches()每行。然后,它将返回一个带有MatchCollection必须为列项的数量相同。每场比赛的价值属性,显然,分析得到的值。

As you can see, you need to call regex.Matches() per line. It will then return a MatchCollection with the same number of items you have as columns. The Value property of each match is, obviously, the parsed value.

这是仍在进展中的工作,但它高兴地解析CSV字符串,如:

This is still a work in progress, but it happily parses CSV strings like:

2,3.03,"Hello, my name is ""Joshua""",A,B,C,,,D