正则表达式匹配偶数双引号("),而不匹配单个CSV
本文关键字:quot CSV 单个 不匹配 正则表达式 | 更新日期: 2023-09-27 18:04:15
我必须编写一个正则表达式来解析每个CSV行。例如,regex匹配的是包含偶数双引号(")而不是单引号的双引号字符串。
例如,CSV分隔符为tab, 't。我有这样一行:
"first column ""end"'tsecond column't"third 'nNewLine'rcolumn'tend"
正则表达式将允许我提取如下三列:
first column ""end
second column
third 'nNewLine'rcolumn'tend
请注意,第一列有两个双引号,但它可以允许偶数双引号。
请注意,第三列中有't,还有'n和'r。
第1列和第3列可以加引号,如果这样写regex更容易。
任何想法?
当且仅当后面有偶数引号时如何分割制表符?
splitArray = Regex.Split(subject,
@"'t # Match a tab
(?= # if the following regex matches after it:
(?: # Match...
[^""]*"" # Any number of non-quotes, followed by a quote
[^""]*"" # ditto, to ensure an even number of quotes
)* # Repeat as many times as needed
[^""]* # Then match any remaining non-quote characters
$ # until the end of the string.
) # End of lookahead assertion",
RegexOptions.IgnorePatternWhitespace);