Suppose I have a global substitution like this: s/this/that/g.

I need to capture $&, $-[0], etc. of each match. Unfortunately, this gives me only the last set:

while (s/this/that/g){...}

And this starts the matching at the beginning of the string each time:

whie (s/this/that/){...}

Is there a way to stop and evaluate each time the /g matches (without using the /e modifier)?

Thanks. I must have read right through the docs regarding the \G assertion. That should do the trick.

>>>>Just as a matter of curiosity, what is the reason for this requirement?

We have a legacy system in which perl substitutions (thousands of them) 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. Of course we don't want to rewrite all these substitutions: we can have the engine manipulate the regex strings a bit as it reads them in, but not make major changes.


In reply to 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.