From: walrus06437@... (Walt Rarus) Date: 2003-01-01T04:03:26+09:00 Subject: Re: How to translate this Perl regex Helmut Leitner wrote in message news:<3E116DDE.972A9081@chello.at>... > Someone said that Ruby can do anything that Perl does. > How would you write this Perl regex > > $s =~ s/(pat1+)pat2(pat3*)/&foo($1,$2)/ge; > > in Ruby? > str = "There are 3*33 bottles of beer on the wall." def foo(a,o,b) case o when '+' then (a.to_i + b.to_i).to_s when '-' then (a.to_i - b.to_i).to_s when '*' then (a.to_i * b.to_i).to_s when '/' then (a.to_i / b.to_i).to_s end end p str.gsub(/^(\D+)(\d+)\s*([+\-*\/])\s*(\d+)(\D+)/) {$1+foo($2,$3,$4)+$5}