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

I've also been trying to interprete the output from use re 'debug';, but the message "Match possible, but length=0 is smaller than requested=1, failing!" has me stumped.

But I did find a workaround for the problem. Adding a non-greedy 0 or 1 quantifier to the code assertion prevents it from being executed twice:

my $rxBetween = qr/ (?<= []>})] ) (?= [[<{(] ) (?{ print "'$`'$&'$''" })?? /x;

This simplified (to reduce the trace output) version of your regex now produces

C:\test>junk6 2>junk6.trace Before: (x1)[x2]{x3}(x4) ---------------------------------------- '(x1)''[x2]{x3}(x4)' '(x1)[x2]''{x3}(x4)' '(x1)[x2]{x3}''(x4)' ---------------------------------------- After: (x1)+[x2]+{x3}+(x4)

I've had a go at coming up with an explanation ,from looking at the way it changes the debug trace...but I decided that I am well up on my mental torture quota for this month, so I'll let someone else have that pleasure :)


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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

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 22:07 UTC
    Thank you for your reply. I would never have thought of trying that. I didn't even realise you could use a quantifier on a code block. Doing that along with re debug shows that the code block doesn't execute at the first and successful match but at the second retried match when the engine realises that, although successful, it has already been here before. I wonder why the non-greediness doesn't hold the second time around.

    Exploring quantifiers with code blocks could be very interesting.

    $ perl -le ' > $str = q{abc}; > $str =~ m{(b)(?{print qq{Match $1}}){3}};' Match b Match b Match b $

    Thanks again,

    JohnGG

      I would never have thought of trying that.

      You'll note that I carefully omitted any description of the tortured, and almost certainly erroneous, logic that led me to try such a thing in the first place.

      Since 'discovering' the idea of quantifiers on zero-length assertions, I've been trying to think of a good use for it. Other than simulating a stutter, I haven't thought of anything useful yet :)


      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.