Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Programmatically forcing a regexp to fail

by Yary (Pilgrim)
on Jul 11, 2011 at 19:27 UTC ( [id://913759]=note: print w/replies, xml ) Need Help??


in reply to Programmatically forcing a regexp to fail

I have a similar question, hope it's OK to add on to this old thread.

If I want to create a regular expression that always fails, and fails quickly, will qr/(*FAIL)/ be the best way? Or will it backtrack over each character? Would qr/^(*FAIL)/ be better?

I'm asking because I'm going through many large log files, where I look for any string that matches a "begin", and build up a "end" regexp... and then remove pieces as I come across "ends", which might then make the end regexp back to null. The null case could be checked like:

if ($there_is_an_ending && /$end_pattern/) ...

but it seems cleaner to set $end_pattern=qr/(*FAIL)/ (not use $there_is_an_ending at all)- if that is indeed efficient.

Maybe time for me to learn to use the cool regular expression debugger and see what happens under the hood!

EDIT

Thanks to help from Somni and Tanktalus in the chatterbox-

$  perl -Mre=debug -E '"foo" =~ /(*FAIL)/'
Compiling REx "(*FAIL)"
Final program:
   1: OPFAIL (2)
   2: END (0)
minlen 0
Matching REx "(*FAIL)" against "foo"
   0 <> <foo>                |  1:OPFAIL(2)
                                  failed...
   1 <f> <oo>                |  1:OPFAIL(2)
                                  failed...
   2 <fo> <o>                |  1:OPFAIL(2)
                                  failed...
   3 <foo> <>                |  1:OPFAIL(2)
                                  failed...
Match failed

vs

$  perl -Mre=debug -E '"foo" =~ /^(*FAIL)/'
Compiling REx "^(*FAIL)"
Final program:
   1: BOL (2)
   2: OPFAIL (3)
   3: END (0)
anchored(BOL) minlen 0
Matching REx "^(*FAIL)" against "foo"
   0 <> <foo>                |  1:BOL(2)
   0 <> <foo>                |  2:OPFAIL(3)
                                  failed...
Match failed

and the winner is

/^(*FAIL)/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://913759]
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 14:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found