开发者

RegEx in Ruby for Converting CSV to TSV

In Ruby, what's a regular expression that identifies commas withi开发者_高级运维n quotes (e.g., "dog, cat, foo, bar")? My purpose is converting a CSV file to TSV, and some of my fields contain strings with commas within quotes that I want to preserve.


Does it have to be a regex? Can just Parse the CSV using your fav csv library, and then rejoin using tabs?

require 'csv'

 test = '"foo,bar,baz",one,two,three'
 CSV.parse_line(test).join("\t")

  "foo,bar,baz\tone\ttwo\tthree"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜