AnomalousMonk has asked for the wisdom of the Perl Monks concerning the following question:
Here's something I don't understand and would appreciate enlightenment about. The (?=(pattern)) regex expression is pretty standard for extracting overlapping sub-strings, character pairs in the example below. However, when combined with embedded eval-ed code, the code is, for some reason unknown to me, evaluated twice. This is the case whether the eval-ed code block is inside or outside of the look-ahead group. Note that the output list of extracted matches is as expected.
The example code functions identically in ActiveState 5.8.9 and Strawberries 5.10.1.5 and 5.12.3.0, and also behaves identically if $1 is used instead of $^N.
Many thanks in advance for any insight on this question.
>perl -wMstrict -le "my $s = 'abcd'; ;; my @pairs1 = $s =~ m{ (?= (..)) (?{ printf qq{'$^N' } }) }xmsg; print ''; printf qq{:$_: } for @pairs1; print ''; ;; my @pairs2 = $s =~ m{ (?= (..) (?{ printf qq{'$^N' } })) }xmsg; print ''; printf qq{:$_: } for @pairs2; print ''; " 'ab' 'ab' 'bc' 'bc' 'cd' 'cd' :ab: :bc: :cd: 'ab' 'ab' 'bc' 'bc' 'cd' 'cd' :ab: :bc: :cd:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex: Overlapping Matches: Double Execution of Eval-ed Code (pos==pos && len!=len)
by tye (Sage) on Jan 15, 2012 at 20:16 UTC | |
by AnomalousMonk (Archbishop) on Jan 15, 2012 at 21:46 UTC | |
|
Re: Regex: Overlapping Matches: Double Execution of Eval-ed Code
by BrowserUk (Patriarch) on Jan 15, 2012 at 20:32 UTC | |
|
Re: Regex: Overlapping Matches: Double Execution of Eval-ed Code
by JavaFan (Canon) on Jan 15, 2012 at 21:12 UTC |