in reply to Why doesn't this regex work? (Solved!)

I think I got it, there is one \s and it is outside of the lookaround, so it gets consumed (advances pos), and backtracking doesn't go back before it, so its checking every other number

Look at the pos, in your version (\s outside (?=)) it jumps 7/15 but with \s inside (?=) it jumps 7/11/15

#! perl -slw use strict; my $data = '105 106 107 108 109 110 111 112 113 1115 1116 1117 1118 1119 1120 1121 1122 1123 12345 12346 12347 12348 12349 12350 12351 12353 '; { my $other = @ARGV ? qr{(?=\s(\d+)\d\s)} : qr{\s(?=(\d+)\d\s)}; open my($DATA),'<',\$data; my $what = ""; while( readline $DATA ) { s{ \s(\d+)\d \K $other }{ warn "WHAT($1)($2)POS(@{[pos()]})\n"; $1 + 1 == $2 ? ";\n" : '! ' }gex; print; } print "\n$what"; } __END__ $ perl junk WHAT(10)(10)POS(7) WHAT(10)(10)POS(15) WHAT(11)(11)POS(23) WHAT(11)(11)POS(31) 105 106! 107 108! 109 110! 111 112! 113 WHAT(111)(111)POS(9) WHAT(111)(111)POS(19) WHAT(112)(112)POS(29) WHAT(112)(112)POS(39) 1115 1116! 1117 1118! 1119 1120! 1121 1122! 1123 WHAT(1234)(1234)POS(11) WHAT(1234)(1234)POS(23) WHAT(1235)(1235)POS(35) 12345 12346! 12347 12348! 12349 12350! 12351 12353 $ perl junk lookaround WHAT(10)(10)POS(7) WHAT(10)(10)POS(11) WHAT(10)(10)POS(15) WHAT(10)(11)POS(19) WHAT(11)(11)POS(23) WHAT(11)(11)POS(27) WHAT(11)(11)POS(31) 105 106! 107! 108! 109; 110! 111! 112! 113 WHAT(111)(111)POS(9) WHAT(111)(111)POS(14) WHAT(111)(111)POS(19) WHAT(111)(112)POS(24) WHAT(112)(112)POS(29) WHAT(112)(112)POS(34) WHAT(112)(112)POS(39) 1115 1116! 1117! 1118! 1119; 1120! 1121! 1122! 1123 WHAT(1234)(1234)POS(11) WHAT(1234)(1234)POS(17) WHAT(1234)(1234)POS(23) WHAT(1234)(1235)POS(29) WHAT(1235)(1235)POS(35) WHAT(1235)(1235)POS(41) 12345 12346! 12347! 12348! 12349; 12350! 12351! 12353

rxrx gave me the idea to pos it

Replies are listed 'Best First'.
Re^2: Why doesn't this regex work? ( lookaround backtracking pos)
by BrowserUk (Patriarch) on Aug 15, 2013 at 11:17 UTC
    there is one \s and it is outside of the lookaround ... so its checking every other number

    BINGO! Thank you.

    If I put the first space into a lookbehind: s[(?<=\s)(\d+)\d\K\s(?=(\d+)\d\s)]{

    Or substitute the zero length \b--which will serve the same purpose: s[\b(\d+)\d\K\s(?=(\d+)\d\s)]{

    The substitution works as I wanted it to.

    But damn I could not see that for looking.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Took me a few dozen looks, I started to compose some funny(wrong) answers some 3-4 times

      The experimental (??{ code }) feature looks kinda neat

      perl -le " $_ = shift; s{(\d+)\d\s\K(?=(??{$1+1})\d\s)}{\n}g; print " +"11 12 21 22 32 33 41 44" 11 12 21 22 32 33 41 44
        Took me a few dozen looks, I started to compose some funny(wrong) answers some 3-4 times

        Makes me feel a little better that I'm not the only one had trouble seeing it :)

        The experimental (??{ code }) feature looks kinda neat

        Indeed. Trouble is I can never remember whether it is the (?{}) or (??{}) that interpolates; and rather than go look it up, I opt for an alternative if one springs to mind.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.