From: Albert Wagner Date: 2001-10-15T09:13:10+09:00 Subject: [ruby-talk:22539] Re: extracting from delimited text files Cool! That really works. Thanks, Bill. All I have available at the moment is the pickaxe book. The only mention of a use for "?" in Chpt 5 is as a repetition modifier. I just found the extensions in Chpt 18. But, seriously, I don't think I could have figured out a use for them without an example. Thanks again. On Sunday 14 October 2001 06:17 pm, you wrote: > Hi Albert, > > From: "Albert Wagner" > > > Thank you, Robert. I was unaware of both the csv module and the Ruby > > Cookbook. I got this far with String#split but couldn't get rid of the > > quote in element 0 and how to force empty strings for the trailing > > commas: > > > > irb(main):048:0> x > > "\"aaa\",,\"ccc\",,,\"fff\",,," > > irb(main):049:0> y = x.split(/\"\s*,\s*\"|\"\s*,|\s*,\s*\"|\s*,|\s*\z/) > > ["\"aaa", "", "ccc", "", "", "fff" > > You can keep the trailing empty fields by passing a 'negative limit' > into split, e.g. y = x.split(/regexp/, -1). However, the elimination > of the leading quote on the first field seemed like a stumper (without > producing an unwanted initial empty field.) Here's a regexp solution > using String#scan . . . > > require 'runit/testcase' > require 'runit/cui/testrunner' > > def csv_split(str) > > str.scan(/(?:\A|,)\s*"([^"]*)"|(?:\A|,)([^",]*)(?=,|\z)/).flatten!.compact! > end > > class TestCsvSplit < RUNIT::TestCase > def testCsvSplit > fields = csv_split(%Q{"aaa",,"c,d,",,,"fff",,,}) > assert fields == ["aaa", "", "c,d,", "", "", "fff", "", "", ""] > end > end > > RUNIT::CUI::TestRunner.run(TestCsvSplit.suite) > > > Hope this covers all the cases. :-) BTW this doesn't handle > "embedded quotes" within a quoted string. That is, the example > you provided didn't specify how or whether embedded quotes > within quoted strings in the CSV fields were treated. This > regexp (above) won't deal with quotes [escaped somehow] inside > a quoted string. > > Hope this helps, > > Bill