From: "David A. Black" Date: 2008-07-13T06:31:51+09:00 Subject: Re: regular expressions help Hi -- On Sun, 13 Jul 2008, phlip wrote: > 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! I don't think the OP wanted the literal quotation marks as part of the results, though. In other words you'd want the third string to be: hello world rather than "hello world" David -- 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!