Helmut Leitner <helmut.leitner / chello.at> 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}