in reply to Loop through global substitution

... perl substitutions ... are kept as lists in separate files and are read in by a processing engine, which then essentially does this: eval $mySubstitution. We now need to keep track of what was matched and their positions in the document.

I think the approaches based on \G already posted will serve perfectly well. However, I cannot help thinking the original eval-based process (insofar as I understand it) could easily be extended.

I imagine the process described above as in the first code sequence below: regex strings and replacement strings wind up in an array or arrays; a loop works through the array(s) eval-ing a  s/// statement for each substitution.

This could easily be extended as in the second code sequence below. No change whatsoever to either regex string or replacement string seems necessary.

>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' ] ];