From: phlip Date: 2008-07-13T06:20:43+09:00 Subject: Re: regular expressions help 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