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)/

In reply to Re: Programmatically forcing a regexp to fail by Yary
in thread Programmatically forcing a regexp to fail by suaveant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.