From: George Ogata Date: 2007-03-17T14:53:26+09:00 Subject: Re: Splitting A String On 3/17/07, Andrew Stewart wrote: > Hello, > > What's a (good) way to convert this: > > 'a quick "brown fox" jumped "over the lazy" dog' > > into this: > > [ 'a', 'quick', 'brown fox', 'jumped', 'over the lazy', 'dog' ] ? > > Thanks! If you're looking for shell-quoting-like behavior, I actually think it's more appropriate to use shellwords for this: irb(main):001:0> require 'shellwords' => true irb(main):002:0> Shellwords.shellwords 'a quick "brown fox" jumps "over the lazy" dog' => ["a", "quick", "brown fox", "jumps", "over the lazy", "dog"] It will also handle sloshing: irb(main):003:0> Shellwords.shellwords 'a\ b c' => ["a b", "c"] And distinguish between single and double quotes (for better or for worse): irb(main):004:0> Shellwords.shellwords %{"a\\"a"} => ["a\"a"] irb(main):005:0> Shellwords.shellwords %{'a\\'} => ["a\\"] Shellwords is in the standard library. Regards, George.