You can get some insight into how this happens in your code with use re 'debug'. I've altered the target string to {x1}[x2] for simplicity.

After several unsuccessful attempts, the engine finds a match starting at position 4:

Setting an EVAL scope, savestack=25 4 <{x1}> <[x2]> | 1: IFMATCH[-1] 3 <{x1> <}[x2]> | 3: OPEN1 3 <{x1> <}[x2]> | 5: ANYOF[)>\]}] 4 <{x1}> <[x2]> | 16: CLOSE1 4 <{x1}> <[x2]> | 18: SUCCEED could match... 4 <{x1}> <[x2]> | 20: IFMATCH[-0] 4 <{x1}> <[x2]> | 22: OPEN2 4 <{x1}> <[x2]> | 24: ANYOF[(<[{] 5 <{x1}[> <x2]> | 35: CLOSE2 5 <{x1}[> <x2]> | 37: SUCCEED could match... 4 <{x1}> <[x2]> | 39: EVAL re_eval 0x10033008 Match 1: left }, right [ 4 <{x1}> <[x2]> | 41: END Match successful!

Then it starts again at position 4, and matches again but then throws it away:

Setting an EVAL scope, savestack=37 4 <{x1}> <[x2]> | 1: IFMATCH[-1] 3 <{x1> <}[x2]> | 3: OPEN1 3 <{x1> <}[x2]> | 5: ANYOF[)>\]}] 4 <{x1}> <[x2]> | 16: CLOSE1 4 <{x1}> <[x2]> | 18: SUCCEED could match... 4 <{x1}> <[x2]> | 20: IFMATCH[-0] 4 <{x1}> <[x2]> | 22: OPEN2 4 <{x1}> <[x2]> | 24: ANYOF[(<[{] 5 <{x1}[> <x2]> | 35: CLOSE2 5 <{x1}[> <x2]> | 37: SUCCEED could match... 4 <{x1}> <[x2]> | 39: EVAL re_eval 0x10033008 Match 2: left }, right [ 4 <{x1}> <[x2]> | 41: END Match possible, but length=0 is smaller than requested=1, failing!
The whole output looks like this:
Compiling REx `[]>})]' size 12 Got 100 bytes for offset annotations. first at 1 1: ANYOF[)>\]}](12) 12: END(0) stclass `ANYOF[)>\]}]' minlen 1 Offsets: [12] 1[6] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 7[0] Compiling REx `[[<{(]' size 12 Got 100 bytes for offset annotations. first at 1 1: ANYOF[(<[{](12) 12: END(0) stclass `ANYOF[(<[{]' minlen 1 Offsets: [12] 1[6] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 7[0] Compiling REx ` (?<=((?-xism:[]>})]))) (?=((?-xism:[[<{(]))) (?{print qq{Match @{ [++ $count] }: left $1, right $2\n}}) ' size 41 Got 332 bytes for offset annotations. first at 1 1: IFMATCH[-1](20) 3: OPEN1(5) 5: ANYOF[)>\]}](16) 16: CLOSE1(18) 18: SUCCEED(0) 19: TAIL(20) 20: IFMATCH[-0](39) 22: OPEN2(24) 24: ANYOF[(<[{](35) 35: CLOSE2(37) 37: SUCCEED(0) 38: TAIL(39) 39: EVAL(41) 41: END(0) minlen 0 with eval Offsets: [41] 9[17] 0[0] 9[1] 0[0] 18[6] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] + 0[0] 0[0] 25[1] 0[0] 25[0] 25[0] 34[17] 0[0] 34[1] 0[0] 43[6] 0[0] 0 +[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 50[1] 0[0] 50[0] 50[0] 56 +[59] 0[0] 115[0] Before: {x1}[x2] ---------------------------------------- Matching REx ` (?<=((?-xism:[]>})]))) (?=((?-xism:[[<{(]))) (?{print qq{Match @{ [++ $count] }: left $1, right $2\n}}) ...' against `{x1}[x2]' Setting an EVAL scope, savestack=25 0 <> <{x1}[x2]> | 1: IFMATCH[-1] failed... Setting an EVAL scope, savestack=25 1 <{> <x1}[x2]> | 1: IFMATCH[-1] 0 <> <{x1}[x2]> | 3: OPEN1 0 <> <{x1}[x2]> | 5: ANYOF[)>\]}] failed... failed... Setting an EVAL scope, savestack=25 2 <{x> <1}[x2]> | 1: IFMATCH[-1] 1 <{> <x1}[x2]> | 3: OPEN1 1 <{> <x1}[x2]> | 5: ANYOF[)>\]}] failed... failed... Setting an EVAL scope, savestack=25 3 <{x1> <}[x2]> | 1: IFMATCH[-1] 2 <{x> <1}[x2]> | 3: OPEN1 2 <{x> <1}[x2]> | 5: ANYOF[)>\]}] failed... failed... Setting an EVAL scope, savestack=25 4 <{x1}> <[x2]> | 1: IFMATCH[-1] 3 <{x1> <}[x2]> | 3: OPEN1 3 <{x1> <}[x2]> | 5: ANYOF[)>\]}] 4 <{x1}> <[x2]> | 16: CLOSE1 4 <{x1}> <[x2]> | 18: SUCCEED could match... 4 <{x1}> <[x2]> | 20: IFMATCH[-0] 4 <{x1}> <[x2]> | 22: OPEN2 4 <{x1}> <[x2]> | 24: ANYOF[(<[{] 5 <{x1}[> <x2]> | 35: CLOSE2 5 <{x1}[> <x2]> | 37: SUCCEED could match... 4 <{x1}> <[x2]> | 39: EVAL re_eval 0x10033008 Match 1: left }, right [ 4 <{x1}> <[x2]> | 41: END Match successful! Matching REx ` (?<=((?-xism:[]>})]))) (?=((?-xism:[[<{(]))) (?{print qq{Match @{ [++ $count] }: left $1, right $2\n}}) ...' against `[x2]' Setting an EVAL scope, savestack=37 4 <{x1}> <[x2]> | 1: IFMATCH[-1] 3 <{x1> <}[x2]> | 3: OPEN1 3 <{x1> <}[x2]> | 5: ANYOF[)>\]}] 4 <{x1}> <[x2]> | 16: CLOSE1 4 <{x1}> <[x2]> | 18: SUCCEED could match... 4 <{x1}> <[x2]> | 20: IFMATCH[-0] 4 <{x1}> <[x2]> | 22: OPEN2 4 <{x1}> <[x2]> | 24: ANYOF[(<[{] 5 <{x1}[> <x2]> | 35: CLOSE2 5 <{x1}[> <x2]> | 37: SUCCEED could match... 4 <{x1}> <[x2]> | 39: EVAL re_eval 0x10033008 Match 2: left }, right [ 4 <{x1}> <[x2]> | 41: END Match possible, but length=0 is smaller than requested=1, failing! Clearing an EVAL scope, savestack=37..40 Setting an EVAL scope, savestack=37 5 <{x1}[> <x2]> | 1: IFMATCH[-1] 4 <{x1}> <[x2]> | 3: OPEN1 4 <{x1}> <[x2]> | 5: ANYOF[)>\]}] failed... failed... Setting an EVAL scope, savestack=37 6 <{x1}[x> <2]> | 1: IFMATCH[-1] 5 <{x1}[> <x2]> | 3: OPEN1 5 <{x1}[> <x2]> | 5: ANYOF[)>\]}] failed... failed... Setting an EVAL scope, savestack=37 7 <{x1}[x2> <]> | 1: IFMATCH[-1] 6 <{x1}[x> <2]> | 3: OPEN1 6 <{x1}[x> <2]> | 5: ANYOF[)>\]}] failed... failed... Setting an EVAL scope, savestack=37 8 <{x1}[x2]> <> | 1: IFMATCH[-1] 7 <{x1}[x2> <]> | 3: OPEN1 7 <{x1}[x2> <]> | 5: ANYOF[)>\]}] 8 <{x1}[x2]> <> | 16: CLOSE1 8 <{x1}[x2]> <> | 18: SUCCEED could match... 8 <{x1}[x2]> <> | 20: IFMATCH[-0] 8 <{x1}[x2]> <> | 22: OPEN2 8 <{x1}[x2]> <> | 24: ANYOF[(<[{] failed... failed... Match failed ---------------------------------------- After: {x1}+[x2] Freeing REx: `"[]>})]"' Freeing REx: `"[[<{(]"' Freeing REx: `"\n (?<=((?-xism:[]>})])))\n (?=((?-xism:[[<{(])))\n + ("......'

In reply to Re: Regex code block executes twice per match using look-arounds by mrpeabody
in thread Regex code block executes twice per match using look-arounds by johngg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.