Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Programmatically forcing a regexp to fail

by mwah (Hermit)
on Oct 31, 2007 at 20:56 UTC ( [id://648361]=note: print w/replies, xml ) Need Help??


in reply to Programmatically forcing a regexp to fail

that you could force a failure of the regexp

It's probably the 'empty negative lookahead' (?!) :

... my @nums = 1 .. 30; my $reg = qr{^(\d+)$ (??{ $1 % 3 ? '(?!)' : '' })}x; print join ',', grep /$reg/, @nums; ...

This is documented eg. in Jeff Pinyan's work (also here on perlmonks).

Correction 1: Added bounds "^ $" to \d+ to prevent backtracking in the above example.

Correction 2: backtracking can be prevented by the atomic group (?>...):

... my $reg = qr{ ^((?>\d+)) (??{ !!($1 % 3) and '(?!)' }) }x; ...

Regards

mwa

Replies are listed 'Best First'.
Re^2: Programmatically forcing a regexp to fail
by suaveant (Parson) on Nov 05, 2007 at 15:50 UTC
    I don't think that's what I had seen before but it works. Thanks! :)

                    - Ant
                    - Some of my best work - (1 2 3)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found