문제

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