From: Vincent Fourmond Date: 2007-02-27T21:31:58+09:00 Subject: Re: Regular expression matches last occurrence instead of first andyo wrote: > Here's the sample code; note that (.*?) and ([^"]+) behave the same > way--and not the way I'd expect: > > str = '"aaaaa""bbb""ccc"' > > str.scan(/"(.*?)"/) > puts $1 > # ccc Normal... #scan is not what you'r looking for: ------------------------------------------------------------ String#scan str.scan(pattern) => array str.scan(pattern) {|match, ...| block } => str ------------------------------------------------------------------------ Both forms iterate through str, matching the pattern (which may be a Regexp or a String). For each match, a result is generated and either added to the result array or passed to the block. [...] scan find all successive matches for the pattern, and sets the captured groups variables everytime it finds one. So, here, you simply get the $1 for the last match, ie "ccc". What you're looking for is simply =~, as in Perl: irb(main):001:0> str = '"aaaaa""bbb""ccc"' => "\"aaaaa\"\"bbb\"\"ccc\"" irb(main):002:0> str =~ /"(.*?)"/ => 0 irb(main):003:0> $1 => "aaaaa" Cheers, Vincent -- Vincent Fourmond, PhD student (not for long anymore) http://vincent.fourmond.neuf.fr/