From: Robert Klemme Date: 2007-03-16T23:40:19+09:00 Subject: Re: Splitting A String On 16.03.2007 15:27, Xavier Noria wrote: > On Mar 16, 2007, at 3:21 PM, Xavier Noria wrote: > >> puts str.scan(/"((?:\\.|[^"])*)"|(\w+)/).flatten.compact > > Sorry, that regexp was part of a test and got copied by accident. I just > meant to clean up the select, that's: > > str = 'a quick "brown fox" jumped "over the lazy" dog' > puts str.scan(/"([^"]*)"|(\w+)/).flatten.compact And solutions with #inject: irb(main):010:0> require 'enumerator' => true irb(main):011:0> str = 'a quick "brown fox" jumped "over the lazy" dog' => "a quick \"brown fox\" jumped \"over the lazy\" dog" irb(main):012:0> str.to_enum(:scan, /"([^"]*)"|(\S+)/).inject([]) {|a,m| a << m.compact!.shift} => ["a", "quick", "brown fox", "jumped", "over the lazy", "dog"] irb(main):013:0> str.to_enum(:scan, /"([^"]*)"|(\S+)/).inject([]) {|a,(m,n)| a << (m||n)} => ["a", "quick", "brown fox", "jumped", "over the lazy", "dog"] But honestly, I found Jan's solution much more elegant. Great stuff! Kind regards robert