开发者

RegEx in Ruby for Converting CSV to TSV

开发者 https://www.devze.com 2023-01-29 18:12 出处:网络
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 strin

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

精彩评论

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