[tpm] looking for a regex that returns true on no match

Fulko Hew fulko.hew at gmail.com
Thu Mar 12 07:15:17 PDT 2009


Following up on my own post.  the following _does_ work:

    if (/^(?!normal)/) {
        print "not normal\n";
    } else {
        print "normal\n";
    }

The important things to note are:
I had to have something in front of the negative lookahead (hence the '^')
but I had originally tried binding to both the front and the back,
but that didn't work.

To summarize:

/(?!normal)/     doesn't work, probably because you can't do a lookahead
                 without defining what to 'lookahead after'
/^(?!normal)/    works (Yippee)
/^(?!normal)$/   doesn't work ;-/

Here was my sample harness for testing:

#!/usr/bin/perl

sub xxx {
    $_ = shift;

    print "'$_' --> ";
    if (/^(?!normal)/) {
        print "not normal\n";
    } else {
        print "normal\n";
    }
}

xxx ("disconnected-unknown");
xxx ("disconnected-timed-out");
xxx ("disconnected-empty-response");
xxx ("disconnected-bad-gw-status");
xxx ("connected-1st-login");
xxx ("connected-nth-login");
xxx ("normal");


and the result I wanted to see:

'disconnected-unknown' --> not normal
'disconnected-timed-out' --> not normal
'disconnected-empty-response' --> not normal
'disconnected-bad-gw-status' --> not normal
'connected-1st-login' --> not normal
'connected-nth-login' --> not normal
'normal' --> normal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20090312/8a2e8267/attachment.html>


More information about the toronto-pm mailing list