Why does this positive wait expression not work when snapping to the beginning of a line?

Why does this statement not work when it is bound to the front of the line? Run the following code and you will see that the first test passes, and the second, which is changed only with the help of the binding ^, fails.

use Test::More tests => 2;

my $s = '/123/456/hello';    
$s =~ s{(?<=/)\d+(?=/\d+/hello)}{0};  # unanchored
is($s, '/0/456/hello', 'unanchored'); # passes

$s = '/123/456/hello';
$s =~ s{^(?<=/)\d+(?=/\d+/hello)}{0}; # anchored
is($s, '/0/456/hello', 'anchored');   # fails

Moving ^to a look-behind statement is not an option for me (this is a very simplified example), but it fixes the problem. I found an alternative way to do what I want, but I am curious why this approach did not work. I tested this on perl 5.8.8 and perl 5.10.0.

+3
source share
4 answers

, . , , .

+9

(?<=/)\d+(?=/hello) 456, , lookarounds. , -. , " , , ", , , .

+6

, (? <= ( ), . , (? .

+4

, lookbehind , ^.

+1
source

Source: https://habr.com/ru/post/1756977/


All Articles