>perl -wMstrict -le "my @search = qw(foo fee fie foe ); my @replace = qw(bar fum \u$& \U$&); ;; my $document = 'the foo is fee and fie-foe.'; ;; while (my ($i, $rx) = each @search) { eval qq{ \$document =~ s{$rx}{$replace[$i]}g }; } print qq{'$document'}; ;; $document = 'all foo are fee or fie/foe'; ;; my @replaced; while (my ($i, $rx) = each @search) { eval qq{ \$document =~ s{$rx} { push \@replaced, [\$&,\$-[0],\$+[0]]; qq{$replace[$i]} }eg }; } print qq{'$document'}; use Data::Dumper; print Dumper \@replaced; " 'the bar is fum and Fie-FOE.' 'all bar are fum or Fie/FOE' $VAR1 = [ [ 'foo', '4', '7' ], [ 'fee', '12', '15' ], [ 'fie', '19', '22' ], [ 'foe', '23', '26' ] ];