Robert Klemme
3/19/2008 7:05:00 PM
On 19.03.2008 17:07, Peter Bunyan wrote:
> Sebastian Hungerecker wrote:
>> Peter Bunyan wrote:
>>> Is there already a built-in method to do this
>> require 'shellwords'
>> Shellwords.shellwords 'Hello "wo rld" how "are \" you today"'
>> => ["Hello", "wo rld", "how", "are \" you today"]
>>
>> HTH,
>> Sebastian
>
> Mmm, that's yummy! Thanks, I'm using this now. Thanks to all of your
> replies, you all rock hard.
Though I'm coming in late to the party: sometimes you can exchange
#split and #scan. This is something I use sometimes:
irb(main):006:0> string = 'a simple solution that uses "a regular
expression" - see?'
=> "a simple solution that uses \"a regular expression\" - see?"
irb(main):007:0> quotes = string.scan %r{"[^"]*"|\S+}
=> ["a", "simple", "solution", "that", "uses", "\"a regular
expression\"", "-", "see?"]
Note: the order of the alternative in the regexp matters! This works
because Ruby's regex engine is NFA based. Here's what happens if you
exchange the two
irb(main):008:0> quotes = string.scan %r{\S+|"[^"]*"}
=> ["a", "simple", "solution", "that", "uses", "\"a", "regular",
"expression\"", "-", "see?"]
Kind regards
robert