On Jan 6, 2006, at 6:08 PM, 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.

I agree that CSV is the way to go, but here's a direct attempt:

 >> example = %Q{some words "some quoted text" some more words}
=> "some words \"some quoted text\" some more words"
 >> example.scan(/\s+|\w+|"[^"]*"/).
?>         reject { |token| token =~ /^\s+$/ }.
?>         map { |token| token.sub(/^"/, "").sub(/"$/, "") }
=> ["some", "words", "some quoted text", "some", "more", "words"]

Hope that gives you some fresh ideas.

James Edward Gray II