Frage

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.

War es hilfreich?

Lösung

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)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top