From: James Edward Gray II Date: 2005-10-29T12:06:51+09:00 Subject: Re: csv.rb a start on refactoring. On Oct 28, 2005, at 8:43 PM, Ara.T.Howard wrote: >>> can you post the optimized re? >>> >> >> Yes. I have to run out right now, but I will post it at some >> point this weekend... >> > > great. Here's the hyper-optimized version with an example of how he uses it (translated from Perl to Ruby by me): >> data = %Q{Ten Thousand,10000, 2710 ,,"10,000","It's ""10 Grand"", baby",10K} => "Ten Thousand,10000, 2710 ,,\"10,000\",\"It's \"\"10 Grand\"\", baby\",10K" >> results = Array.new => [] >> data.scan(/\G(?:^|,)(?:"((?>[^"]*)(?>""[^"]*)*)"|([^",]*))/) do ?> if $1.nil? >> results << $2 >> else ?> results << $1.gsub(%Q{""}, %Q{"}) >> end >> end => "Ten Thousand,10000, 2710 ,,\"10,000\",\"It's \"\"10 Grand\"\", baby\",10K" >> results => ["Ten Thousand", "10000", " 2710 ", "", "10,000", "Ten Thousand", "10000", " 2710 ", "", "10,000", "It's \"10 Grand\", baby", "10K"] James Edward Gray II