From: Geoff Jacobsen Date: 2006-01-09T19:31:35+09:00 Subject: Re: Splitting strings on spaces, unless inside quotes 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 "]