Hi --
Axel wrote:
str = 'hi hello "hello world" hey yo'
str.gsub!( / \" [^\"]* \" /x ) {|e| e[1..-2].gsub(' ', "\007") }
result = str.scan( / [\w\007]+ /x ).map {|e| e.gsub("\007", " ") }
p result
str = 'hi hello "hello world" hey yo'
p str.scan(/(".*")|(\w+)/).flatten.compact
=> ["hi", "hello", "hello world", "hey", "yo"]
That's not quite the result, though:
>> str = 'hi hello "hello world" hey yo'
=> "hi hello \"hello world\" hey yo"
>> str.scan(/(".*")|(\w+)/).flatten.compact
=> ["hi", "hello", "\"hello world\"", "hey", "yo"]
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 = 'one "two" "three" four'
=> "one \"two\" \"three\" four"
str.scan(/(".*")|(\w+)/).flatten.compact
=> ["one", "\"two\" \"three\"", "four"] # only three strings
Try this:
str.scan(/"([^"]+)"|(\w+)/).flatten.compact
Of course this assumes no embedded/escaped/nested "'s, etc.
David
···
On Sun, 13 Jul 2008, phlip wrote:
--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
Advancing With Rails August 18-21 Edison, NJ
See http://www.rubypal.com for details and updates!