|Do you know where your variables are?|
Regex code block executes twice per match using look-aroundsby johngg (Abbot)
|on Jul 12, 2007 at 10:27 UTC||Need Help??|
johngg has asked for the
wisdom of the Perl Monks concerning the following question:
I've come across something a bit strange when using substitution at a point determined by look-behind and -ahead assertions. I used a regex code block to print some diagnostic text whenever a match occurred but found to my surprise that the code block seemed to be called twice for each match. Here's a small script that demonstrates the behaviour. It is just inserting a plus sign into a string at any point between pseudo-tags with different sorts of bracket pairs.
Even though the code block seems to execute twice per match the substitution is only done once. Here's the output, six apparent matches, weird.
If I change the code so that look-arounds are not used the strange behaviour does not happen and the code block is only executed once per match.
The output this time, just three matches as expected.
I have done other testing to try and pin down what is happening. The code block also executes twice if I am just matching using look-arounds rather than substituting. I have also tried using just a single look-behind rather than both lokk-behind and -ahead but still get the double execution.
Can any Monk throw some light on what is happening here?