texuser74 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I need to add a value while doing a find and replace i.e.
s!total of (.*?) and (.*?)\n!total is $1+$2\n!g;
Please advise how to add $1 and $2, i am getting the string "$1+$2" instead of its total.

Replies are listed 'Best First'.
Re: addition in regular expressions
by ysth (Canon) on Dec 13, 2007 at 09:33 UTC
      hi thanks, your solution did the magic for me.
Re: addition in regular expressions
by moritz (Cardinal) on Dec 13, 2007 at 09:25 UTC
    Use the /e-Modifier:
    s{total of (.*?) and (.*?)\n}{'total is ' . ($1+$2) . '\n!'}eg;