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

In reply to Re: Loop through global substitution by AnomalousMonk
in thread Loop through global substitution by alafarm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.