From: Robert Klemme Date: 2006-01-09T20:53:01+09:00 Subject: Re: Splitting strings on spaces, unless inside quotes Geoff Jacobsen wrote: > On Mon, 2006-01-09 at 18:13 +0900, William James wrote: >> Richard Livsey wrote: >>> I want to split a string into words, but group quoted words together >>> such that... >>> >>> some words "some quoted text" some more words >>> >>> would get split up into: >>> >>> ["some", "words", "some quoted text", "some", "more", "words"] >> >> s = 'some words "some quoted text" some more words' >> p s.split( / *"(.*?)" *| / ) >> > > Which along with the CSV solution can't handle complex cases: > > s='one two" "\'with quotes\' "three "' > > s.split( / *"(.*?)" *| / ) > => ["one", "two", " ", "'with", "quotes'", "three "] > > require 'csv' > CSV::parse_line(s) > => [] > > but Shellwords can: > > require 'shellwords' > Shellwords.shellwords(s) > => ["one", "two with quotes", "three "] Another option is to use scan instead of split: >> 'some words "some quoted text" some more words'.scan %r{"(?:(?:[^"]|\\.)*)"|\S+} => ["some", "words", "\"some quoted text\"", "some", "more", "words"] With some additional effort even the quotes can be removed (using grouping for example). >> r=[];'some words "some quoted text" some more words'.scan(%r{"((?:[^"]|\\.)*)"|(\S+)}) {|m| r << m.detect {|x|x}};r => ["some", "words", "some quoted text", "some", "more", "words"] Kind regards robert