I have a string like below

 "\"123\",\"Columbia, Gem Of The Ocean\""

I want to convert it into array and I should get an output like below

["123","Columbia, Gem Of The Ocean"]

But when i use split by comma method on string i get an output like below

 ["\"123\"", "\"Columbia", " Gem Of The Ocean\""]

It is splitting "Columbia, Gem Of The Ocean" by "," but i don't want that.

There is a parse_line method of csv but that doesn't work in ruby 1.9.2.

Please suggest some solution. Thanks in advance.

有帮助吗?

解决方案

Your description and the expected result do not match. You don't want to split it by (all) commas. You want to extract the parts surrounded by double quotation.

string.scan(/".*?"/)

If you don't want the quotations, then

string.scan(/"(.*?)"/).flatten(1)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top