vb.net 拆分字符串并忽略引号内的分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21261314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:31:09  来源:igfitidea点击:

Splitting a string and ignoring the delimiter inside quotes

c#.netregexvb.netcsv

提问by Tachi

I am using .NET's String.Split method to break up a string using commas, but I want to ignore strings enclosed in double quotes for the string. I have read that a

我正在使用 .NET 的 String.Split 方法使用逗号拆分字符串,但我想忽略用双引号括起来的字符串。我读过一个

For example, the string below.

例如,下面的字符串。

Fruit,10,"Bananas, Oranges, Grapes"

I would like to get the following

我想得到以下

Fruit
10
"Bananas, Oranges, Grapes"

Currently I am getting the following output

目前我得到以下输出

Fruit
10
"Bananas
 Oranges
 Grapes"
enter code here

After following suggestions and the answers provided, here is a sample of what I ended up with. (It worked for me obviously)

在遵循建议和提供的答案之后,这里是我最终得到的示例。(它显然对我有用)

Imports Microsoft.VisualBasic.FileIO

Dim fileReader As New TextFieldParser(fileName)

fileReader.TextFieldType = FieldType.Delimited
fileReader.SetDelimiters(",")
fileReader.HasFieldsEnclosedInQuotes = True

While fileReader.EndOfData = False


Dim columnData() As String = fileReader.ReadFields

' Processing of field data

End While

采纳答案by Jerry

You are better off with a parser, like those mentioned in the comments. That said, it's possible to do it with regex in the following way:

你最好使用解析器,就像评论中提到的那样。也就是说,可以通过以下方式使用正则表达式来做到这一点:

,(?=(?:[^"]*"[^"]*")*[^"]*$)

The positive lookahead ((?= ... )) ensures that there is an even number of quotes ahead of the comma to split on (i.e. either they occur in pairs, or there are none).

正向前瞻 ( (?= ... )) 确保在要拆分的逗号之前有偶数个引号(即它们成对出现,或者没有)。

[^"]*matches non-quote characters.

[^"]*匹配非引号字符。

回答by Jitendra G2

I found below is the easiest way, we can do it

我发现下面是最简单的方法,我们可以做到

string fruits = "Fruit,10,"Bananas, Oranges, Grapes"";
string[] fruitsArr = Regex.Split(fruits, ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");

Output:

输出:

fruitsArr[0] = "Fruit"
fruitsArr[1] = "10"
fruitsArr[2] = "\"Bananas, Oranges, Grapes\""

If you need pure string data so you can do it something like,

如果您需要纯字符串数据,那么您可以这样做,

fruitsArr[2].Replace("\"", "")

fruitsArr[2].Replace("\"", "")

回答by SimonGiubs

A quick workaround could be pre-parse the commas inside the quotes and replace them with another delimiter, split the values and post-parse the values with the delimiter replacing it with the original commas.

一个快速的解决方法是预先解析引号内的逗号并将它们替换为另一个分隔符,拆分值并使用分隔符将其替换为原始逗号后解析值。

回答by Praveen Singh

if using c#, you can use

如果使用 c#,你可以使用

        string searchQuery = "Fruit,10,\"Bananas, Oranges, Grapes\"";
        List<string> list1 = Regex.Matches(searchQuery, @"(?<match>\w+)|\""(?<match>[\w\s,]*)""").Cast<Match>().Select(m => m.Groups["match"].Value).ToList();
        foreach(var v in list1)
        Console.WriteLine(v);

Output :

输出 :

Fruit

水果

10

10

Bananas, Oranges, Grapes

香蕉、橙子、葡萄