【问题标题】:Regular expression when quotes appear inside double quotes [duplicate]双引号内出现引号时的正则表达式[重复]
【发布时间】:2022-01-22 07:21:30
【问题描述】:

我正在使用正则表达式在 C# 中拆分字符串。使用的分隔符是“,”。即我需要基于“,”进行拆分。另一个条件是双引号内的“,”不需要考虑。我的正则表达式一直在工作,直到其他双引号出现在双引号内。

表达式

,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))

成功数据:

错误数据:

【问题讨论】:

  • 你为什么不用String.Split()?如果你想在引用字段中解析带有分隔符的 CSV 文件,你应该使用像 CsvHelper 这样的 CSV 解析器。用正则表达式解析 CSV 不容易

标签: c# regex


【解决方案1】:

下面给出了适合我的正则表达式

,(?=(?:[^"])*,)|,(?=["][\w|\W|^",]*",)|,(?![^"]*")|,(?=["][\W|\w]*["])

下图有助于理解上述正则表达式

第 1 节: enter image description here

第 2 节: enter image description here

第 3 节: enter image description here

第 4 节: enter image description here

结果: enter image description here

【讨论】:

    猜你喜欢
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    相关资源
    最近更新 更多