From: William James Date: 2006-01-10T04:23:11+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 "] This is not a "more complex case"; it is an invalid case. The original poster simply wanted to avoid splitting on spaces within double quotes, not within single quotes. The shellwords "solution" is a solution to a different problem, not to this one. It can't even handle a simple case: require 'shellwords' s = "why can't you think?" Shellwords.shellwords(s) ArgumentError: Unmatched single quote: 't you think?