Keep It Simple, Stupid | |
PerlMonks |
Re: Programmatically forcing a regexp to failby Yary (Pilgrim) |
on Jul 11, 2011 at 19:27 UTC ( [id://913759]=note: print w/replies, xml ) | Need Help?? |
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 Section
Seekers of Perl Wisdom
|
|