alafarm has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Loop through global substitution
by moritz (Cardinal) on Jun 20, 2011 at 12:40 UTC | |
|
Re: Loop through global substitution
by AnomalousMonk (Archbishop) on Jun 20, 2011 at 14:05 UTC | |
|
Re: Loop through global substitution
by hbm (Hermit) on Jun 20, 2011 at 13:30 UTC | |
|
Re: Loop through global substitution
by ikegami (Patriarch) on Jun 20, 2011 at 17:48 UTC | |
|
Re: Loop through global substitution
by AnomalousMonk (Archbishop) on Jun 20, 2011 at 22:29 UTC | |
|
Re: Loop through global substitution
by ikegami (Patriarch) on Jun 20, 2011 at 17:41 UTC |