Max*_*ams 3 ruby regex csv split
我在CSV文件中以此行为例:
2412,21,"Which of the following is not found in all cells?","Curriculum","Life and Living Processes, Life Processes",,,1,0,"endofline"
Run Code Online (Sandbox Code Playgroud)
我想把它拆分成一个数组.直接的想法只是在逗号上分开,但是其中一些字符串中有逗号,例如"生命和生活过程,生命过程",这些应该作为数组中的单个元素保留.还要注意,两个逗号之间没有任何内容 - 我希望将它们作为空字符串.
换句话说,我想得到的数组是
[2412,21,"Which of the following is not found in all cells?","Curriculum","Life and Living Processes, Life Processes","","",1,0,"endofline"]
Run Code Online (Sandbox Code Playgroud)
我可以想到涉及eval的hacky方式,但我希望有人能想出一个干净的正则表达式来做...
干杯,最大
这不是正则表达式的合适任务.你需要一个CSV解析器,Ruby有一个内置的:
http://ruby-doc.org/stdlib/libdoc/csv/rdoc/classes/CSV.html
还有一个可以说是优越的第三部分库:
http://fastercsv.rubyforge.org/