c:\@Work\Perl>perl -wMstrict -le "my $operators = '+-*/'; my %disp = map { $_ => eval qq{ sub { return \$_[0] $_ \$_[1] } } } split '', $operators ; my $int = qr{ [-+]? \d+ }xms; my $op = qr{ [\Q$operators\E] }xms; ;; my $t = 'foo 1 + 2 bar 34*56 baz 3- 4 and 56 /78 too'; ;; for my $s ($t, @ARGV) { print ''; print qq{'$s'}; $s =~ s{ ($int) \s* ($op) \s* ($int) }{ $disp{$2}->($1, $3) }xmsge; print qq{'$s'}; } " "x-3--4,-3++4" "foo+bar" 'foo 1 + 2 bar 34*56 baz 3- 4 and 56 /78 too' 'foo 3 bar 1904 baz -1 and 0.717948717948718 too' 'x-3--4,-3++4' 'x1,1' 'foo+bar' 'foo+bar'