RegEx - 解析 Csv 文本

标签 regex vb.net parsing csv

所以这里有很多帖子指出,我应该使用 Vb.Net TextFiledParser,而不是滚动我自己的 csv 解析器。

我尝试过,但是,如果我错了,请告诉我,它会根据单个分隔符进行解析。

因此,如果我有一个地址字段“Flat 1, StackOverflow House, London”,我会得到三个字段。不幸的是,这不是我想要的。我需要给定单元格中的所有内容都作为数组中的单个项目保留。

所以我开始编写自己的正则表达式,如下所示:

var testString = @"""Test 1st string""" + "," + @"""Flat 1, StackOverflow House, London, England, The Earth""" + "," + "123456";

var matches = Regex.Matches(chars, @"""([^""\\])*?(?:\\.[^""\\]*)*?""");
var numbers = Regex.Matches(chars, @"\d+$");//only numbers
Assert.That(results.Count(), Is.EqualTo(3));
Assert.That(secondMatch.Count, Is.EqualTo(1));

第一个断言失败,因为未返回字符串“123456”。该表达式仅返回“Test 1st string”和“Flat 1, StackOverflow House, London, England, The Earth”

我想要的是正则表达式返回所有引用\转义的内容和数字。

我不控制数据,但数字字符串将全部被引用\转义,而数字则不会。

我真的很感谢一些帮助,因为我一直在尝试第三方库但没有取得太大成功。

不用说 string.split 在地址的情况下不起作用,并且 http://www.filehelpers.com/ 似乎没有考虑到这样的例子。

最佳答案

只是为了让您了解您所面临的问题:这是一个应该可以很好地工作的正则表达式。但你肯定需要测试一下它,因为 CSV 有很多极端情况,我肯定错过了一些(我假设逗号作为分隔符," 作为引号字符(通过加倍转义):

(?:           # Match either
 (?>[^",\n]*) #  0 or more characters except comma, quote or newline
|             # or
 "            #  an opening quote
 (?:          #  followed by either
  (?>[^"]*)   #   0 or more non-quote characters
 |            #  or
  ""          #   an escaped quote ("")
 )*           #  any number of times
 "            #  followed by a closing quote
)             # End of alternation
(?=,|$)       # Assert that the next character is a comma (or end of line)

在 VB.NET 中:

Dim ResultList As StringCollection = New StringCollection()
Dim RegexObj As New Regex(
    "(?:            # Match either" & chr(10) & _
    " (?>[^"",\n]*) #  0 or more characters except comma, quote or newline" & chr(10) & _
    "|              # or" & chr(10) & _
    " ""            #  an opening quote" & chr(10) & _
    " (?:           #  followed by either" & chr(10) & _
    "  (?>[^""]*)   #   0 or more non-quote characters" & chr(10) & _
    " |             #  or" & chr(10) & _
    "  """"         #   an escaped quote ("""")" & chr(10) & _
    " )*            #  any number of times" & chr(10) & _
    " ""            #  followed by a closing quote" & chr(10) & _
    ")              # End of alternation" & chr(10) & _
    "(?=,|$)        # Assert that the next character is a comma (or end of line)", 
    RegexOptions.Multiline Or RegexOptions.IgnorePatternWhitespace)
Dim MatchResult As Match = RegexObj.Match(SubjectString)
While MatchResult.Success
    ResultList.Add(MatchResult.Value)
    MatchResult = MatchResult.NextMatch()
End While

关于RegEx - 解析 Csv 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10516740/

相关文章:

c# - 如何确定文件是否与文件掩码匹配?

java - 如何用引号、站点运算符和非引号拆分字符串?

java - 此代码的答案对于每个输入都是错误的,无论它是正确的还是错误的

vb.net - 使用 SQL 参数处理 IN 子句中的数据?

asp.net - 此行已经属于另一个表

sql-server - SQL数据库表批量更新的陷阱,在vb.net中使用临时表和批量复制

parsing - HTTP2 上的 REST API : I need to parse the binary response?

C 程序最后返回一系列问号,有时不返回任何内容

javascript - 如何检查第一个字符是否为字母

c# - 需要一个包含除排除单词之外的任何字符的 C# 正则表达式模式