我知道这个(或类似的)已被多次询问,但尝试了许多可能性,我无法找到一个100%正常的正则表达式.
我有一个CSV文件,我试图将它拆分成一个数组,但遇到两个问题:引用逗号和空元素.
CSV看起来像:
123,2.99,AMO024,Title,"Description, more info",,123987564
Run Code Online (Sandbox Code Playgroud)
我试图使用的正则表达式是:
thisLine.split(/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/)
Run Code Online (Sandbox Code Playgroud)
唯一的问题是在我的输出数组中,第5个元素是123987564,而不是空字符串.
我正在尝试使用正则表达式 (re.split) 拆分字符串,但自从我使用正则表达式以来已经有一段时间了。
字符串看起来像:
string = '"first, element", second element, third element, "fourth, element", fifth element'
Run Code Online (Sandbox Code Playgroud)
我想在每个逗号上拆分字符串,除非子字符串用引号括起来。
输出应如下所示:
output = ['"first, element"', 'second element', 'third element', '"fourth, element"', 'fifth element']
Run Code Online (Sandbox Code Playgroud)