Phlip
7/12/2008 9:22:00 PM
David A. Black wrote:
>> => ["hi", "hello", "hello world", "hey", "yo"]
>
> That's not quite the result, though:
I suspect I copied the wrong line from my transcript!
But...
> The "'s are returned as part of the string '"hello world"'. Also, you
> get the wrong result if you have two quoted strings in a row, because
> of the greediness:
str = 'hi hello "hello world" "hey yo"'
p str.scan(/(?:".*")|(?:\w+)/)
=> ["hi", "hello", "\"hello world\" \"hey yo\""] # bad
p str.scan(/(?:".*?")|(?:\w+)/)
=> ["hi", "hello", "\"hello world\"", "\"hey yo\""] # good!
(-:
> str.scan(/"([^"]+)"|(\w+)/).flatten.compact
The non-greedy matcher .*? looks cuter.
> Of course this assumes no embedded/escaped/nested "'s, etc.
Using regexps as real language parsers makes certain baby deities cry...
--
Phlip