Reputation: 826
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.
Upvotes: 1
Views: 169
Reputation: 168259
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)
Upvotes: 1