From: ts Date: 2000-09-02T22:46:07+09:00 Subject: [ruby-talk:4747] Re: Possible regex bug? >>>>> "D" == Dave Thomas writes: D> So, in this respect, Perl and Ruby do the same thing. There is a difference between perl and ruby. If you look at simple example you'll probably not see it, for example : pigeon% cat b.rb #!/usr/bin/ruby 'foo'.scan(/o?/) do p "match <#$&>" end pigeon% b.rb "match <>" "match " "match " "match <>" pigeon% pigeon% cat a.pl #!/usr/bin/perl -l print "match <$&>" while 'foo' =~ /o?/g; pigeon% pigeon% a.pl match <> match match match <> pigeon% But when you write regexp with non-greedyness you see the difference : pigeon% cat b.rb #!/usr/bin/ruby 'foo'.scan(/o??/) do p "match <#$&>" end p 'bar'.gsub(/\w??/) do |x| "<#{x}>" end pigeon% pigeon% b.rb "match <>" "match <>" "match <>" "match <>" "<>b<>a<>r<>" pigeon% pigeon% cat a.pl #!/usr/bin/perl -l print "match <$&>" while 'foo' =~ /o??/g; $_ = 'bar'; s/\w??/<$&>/g; print; pigeon% a.pl match <> match <> match match <> match match <> <><><><> pigeon% The difference seems because perl has complex rule to resolve zero-length match (a zero-length match don't move the "cursor"). For ruby an expression like /\w??/ in global context is more like : 1) try the next expression in the regexp (in this case return with an empty match) 2) move the "cursor" 3) go to 1 p.s.: the examples are extract from perlre Guy Decoux