Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Regex code block executes twice per match using look-arounds

by mrpeabody (Friar)
on Jul 12, 2007 at 18:54 UTC ( [id://626299]=note: print w/replies, xml ) Need Help??


in reply to Regex code block executes twice per match using look-arounds

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 + ("......'

Replies are listed 'Best First'.
Re^2: Regex code block executes twice per match using look-arounds
by johngg (Canon) on Jul 12, 2007 at 19:39 UTC
    Thank you for your reply. As you can see from my reply to ikegami we have been thinking in parallel. I have tried to get my head around the output from re debug before but found the learning curve very steep. Today in the documentation I spotted use re 'debugcolor'; which gave me enough of a helping hand (even in black and white) to start to penetrate the mystery. I have been meaning to get the Owl Book mentioned by sgt for some time but today has shown that I have a long way to go in my understanding; buying the book is now a priority.

    Cheers,

    JohnGG

      Yeah, it's cryptic. There's some info on interpreting it in perldebguts .

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://626299]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-18 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found