in reply to Forcing a regex to fail

The first problem is that you’re using (?:) to contain the alternation, and then using $+ to check the captured value. Which captured value? There’s no captured value. You need to move the capturing parens from the split to the the qr//.

Next, do you really need a deferred pattern there? I’d write it like so:

qr/($QUOTED|$NUM)(?(?{ '.' eq $+ })$FAIL)/;

(which of course implies variables rather than constants.)

Now given that, you get a zero-length match:

$VAR1 = [
          'name => ',
          '"foo"',
          ', fav',
          '',
          '.num => ',
          '3'
        ]; 

So obviously a match against the lone dot was prevented, but $NUM succeeds in matching nothing. Easily fixed:

qr/($QUOTED|$NUM)(?(?{ '.' eq $+ or not length( $+ ) })$FAIL)/;

Result:

$VAR1 = [
          'name => ',
          '"foo"',
          ', fav.num => ',
          '3'
        ]; 

Q.E.D.

But for practical use I’d prefer tye’s approach, which forces failure without squandering effort.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^2: Forcing a regex to fail
by Ovid (Cardinal) on May 06, 2005 at 15:33 UTC

    You need to move the capturing parens from the split to the the qr//.

    I like your solution, but as noted in the original query, I can't change the split line. tye's solution is the way I've gone. It works quite nicely.

    Cheers,
    Ovid

    New address of my CGI Course.