From: Eero Saynatkari Date: 2006-01-07T10:00:06+09:00 Subject: Re: Splitting strings on spaces, unless inside quotes On 2006.01.07 09:08, 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"] > > So far I'm drawing a blank on the 'Ruby way' to do this and the only > solutions I can think of are turning out to be fairly ugly. > > Any advice would be great. Thanks in advance. Naively, you can try something like this: s = 'foo bar "baz quux" roo' s.scan(/(?:"")|(?:"(.*[^\\])")|(\w+)/).flatten.compact Elaborate as necessary (add support for single quotes or something). > R.Livsey E