From: Robert Klemme Date: 2003-02-21T22:01:41+09:00 Subject: Re: Regexp help: Parsing a CSV file irb(main):087:0* line='first, "se cond", third , four th' "first, \"se cond\", third , four th" irb(main):088:0> rx = /"[^"]*"|'[^']*'|[^,]+/ /"[^"]*"|'[^']*'|[^,]+/ irb(main):089:0> line.scan rx do |match| p match.strip end "first" "\"se cond\"" "third" "four th" "first, \"se cond\", third , four th" irb(main):090:0> robert "Tim Bates" schrieb im Newsbeitrag news:200302212243.48991.tim@bates.id.au... > I've dumped a CSV (comma separated values) file from Excel, and I want to > parse it into cels within my Ruby script. Easy enough, you say: > line.split(',') > But it's a little more complicated than that - see, if the cel has a comma in > it, it gets surrounded in quotes. If a cel has a quote in it, it's surrounded > by quotes and doubled, eg: > Test Te,st Te"st > becomes > Test,"Te,st","Te""st" > Now to use String#split, I would have to write a regexp that will match a > comma, provided that comma is preceeded by an even number of quotes. BUT, I > don't want the regexp to match the quotes themselves, just the comma. I can't > figure this one out... > > Tim Bates > -- > tim@bates.id.au >