in reply to zero-width assertions for extended regular expressions

Another way to think of it is that (zero-width) look-around assertions act from positions between characters, so in the string
                             5 4 W
                                ^^
                                ||
    (?=4) looks forward from here|
                                 |
         at the character (W) here

So is W the same as 4? It is not. (Actually, the regex checks every possible position since it is not constrained to do otherwise, but even so, W is never 4.) And for  (?=\d) likewise.

Update: Also consider:

c:\@Work\Perl\monks>perl -wMstrict -le "my $s = '54W'; ;; print 'match' if $s =~ m{ (?=4) . W }xms; " match

Replies are listed 'Best First'.
Re^2: zero-width assertions for extended regular expressions (rxrx)
by Anonymous Monk on May 27, 2014 at 07:41 UTC

    Good commentary AnomalousMonk :)

    This is where something like rxrx shines over use re 'debug'; as it shows you how the matching goes without the interference of optimizations :)

    So hopefully you can see from rxrx output that zero-width assertions match from current position without advancing that pos()ition . "Look-behind matches text up to the current match position, look-ahead matches text following the current match position." The position is pos() an is advanced by matching patterns

    So in the OPs , pos(0) tries to see ahead if '5' is 4 which its not, the bumps pos(1) and tries to see ahead if '4' is 4 and it is , but then it fails to match a W at pos(1) , because the look-ahead didn't advance pos()ition, and there is a 4 at pos(1) and not a W ..... :)