In addition to those pointed out by Corion and kcott, there's another little problem with the OPed regex. The first .* assertion "consumes" all but one of the \d digits available for \d+ to match, giving a wrong answer to the question of Life, the Universe and Everything:
This problem could be solved by either making the first .* "lazy", as I like to call it:c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $s = qq{DoGcat\n 42\n birdfIsH}; dd $s; print qq{<<$s>>}; ;; print qq{LUE == '$1'} if $s =~ /(?<=dog) .* (\d+) .* (?=fish)/sxi; " "DoGcat\n 42\n birdfIsH" <<DoGcat 42 birdfIsH>> LUE == '2'
or by being more specific about what this group should match:c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $s = qq{DoGcat\n 42\n birdfIsH}; dd $s; print qq{<<$s>>}; ;; print qq{LUE == '$1'} if $s =~ /(?<=dog) .*? (\d+) .* (?=fish)/sxi; " "DoGcat\n 42\n birdfIsH" <<DoGcat 42 birdfIsH>> LUE == '42'
(I'm also not using the useless /g modifier.) (Update: Of course, none of this matters if you don't really care what the answer is, only that there is an answer! :)c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $s = qq{DoGcat\n 42\n birdfIsH}; dd $s; print qq{<<$s>>}; ;; print qq{LUE == '$1'} if $s =~ /(?<=dog) \D* (\d+) .* (?=fish)/sxi; " "DoGcat\n 42\n birdfIsH" <<DoGcat 42 birdfIsH>> LUE == '42'
Give a man a fish: <%-{-{-{-<
In reply to Re: Positive lookbehind and lookahead confusion
by AnomalousMonk
in thread Positive lookbehind and lookahead confusion
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |