in reply to ^N.*(?!00$)

Thanks fellow monks!

Solutions 3 and 4 solved my problem. Which one is better would be a matter of taste i guess.
I expect 3 would be slightly faster.
___________________NZ31200_________NZ3120A___________ 1 ^N.*?[^0][^0]$ NO match NO match 2 ^N.*?00$ match NO match 3 ^N.*(?<!00)$ NO match match 4 ^N(?!.*00$).*$ NO match match 5 N.* and !/00$ won't do, as it must be ONE RegEx ____________________________________________________
-- allan
===========================================================
As the eternal tranquility of Truth reveals itself to us, this very place is the Land of Lotuses
-- Hakuin Ekaku Zenji

Replies are listed 'Best First'.
Re^2: ^N.*(?!00$)
by ikegami (Patriarch) on Jun 06, 2005 at 16:48 UTC
    By the way there's also
    /^N.*(?!00)..$/
    and
    /^N(?!.*00$)/
Re^2: ^N.*(?!00$)
by tlm (Prior) on Jun 06, 2005 at 15:34 UTC

    OK, thanks to ikegami's help, I've fixed the benchmarking code. Here are the results:

    # The suffix +/- correspond to "matching" and "non-matching" input, # respectively. # input length: 7 Rate ?<!- ?!+ ?<!+ ?!- ?<!- 583158/s -- -38% -54% -55% ?!+ 945231/s 62% -- -25% -27% ?<!+ 1262619/s 117% 34% -- -2% ?!- 1286220/s 121% 36% 2% -- # input length: 10 Rate ?<!- ?!+ ?<!+ ?!- ?<!- 220554/s -- -49% -63% -64% ?!+ 430349/s 95% -- -28% -31% ?<!+ 601510/s 173% 40% -- -3% ?!- 619376/s 181% 44% 3% -- # input length: 10000 Rate ?<!- ?!+ ?!- ?<!+ ?<!- 342/s -- -91% -99% -99% ?!+ 3864/s 1029% -- -86% -86% ?!- 26795/s 7730% 593% -- -6% ?<!+ 28417/s 8204% 635% 6% --
    The differences between first and second place are not significant (different runs of the same benchmarking script would yield different relative ordering). The differences between 2nd and 3rd place, and between 3rd and 4th place are significant, though. The results are not conclusive. Negative look-ahead clearly beats positive look-ahead when the input matches, but the opposite is true when the input doesn't match.

    the lowliest monk

Re^2: ^N.*(?!00$)
by ikegami (Patriarch) on Jun 06, 2005 at 17:27 UTC
    Here are some benchmarks: (fixed from TLM's)
    use strict; use warnings; use Benchmark 'cmpthese'; my $sz = ( shift || 10 ) - 4; my $X = 'N' . 'x' x $sz . '000'; my $Y = 'N' . 'x' x $sz . '001'; print("X = $X\n"); print("Y = $Y\n"); print("\n"); cmpthese( -1, { '/^N(?!.*00$).*$/ failure' => sub { scalar $X =~ /^N(?!.*00$).*$/ }, '/^N(?!.*00$).*$/ success' => sub { scalar $Y =~ /^N(?!.*00$).*$/ }, '/^N(?!.*00$)/ failure' => sub { scalar $X =~ /^N(?!.*00$)/ }, '/^N(?!.*00$)/ success' => sub { scalar $Y =~ /^N(?!.*00$)/ }, '/^N.*(?<!00$)$/ failure' => sub { scalar $X =~ /^N.*(?<!00$)$/ }, '/^N.*(?<!00$)$/ success' => sub { scalar $Y =~ /^N.*(?<!00$)$/ }, '/^N.*(?!00)..$/ failure' => sub { scalar $X =~ /^N.*(?!00)..$/ }, '/^N.*(?!00)..$/ success' => sub { scalar $Y =~ /^N.*(?!00)..$/ }, });

    Results:

    X = Nxxxxxx000 Y = Nxxxxxx001 /^N.*(?!00)..$/ failure 278229/s -- /^N.*(?<!00$)$/ failure 330512/s 19% /^N(?!.*00$).*$/ success 536004/s 93% /^N.*(?!00)..$/ success 540980/s 94% /^N(?!.*00$)/ success 633900/s 128% /^N(?!.*00$)/ failure 743994/s 167% /^N(?!.*00$).*$/ failure 750772/s 170% /^N.*(?<!00$)$/ success 789866/s 184%

    Combined success and failure results:
    (If the numbers are evenly distributed, the chance of failure is 0.01.)

    /^N(?!.*00$).*$/ 750772/s * 0.01 + 536004/s * 0.99 = 538151.68/s -- /^N.*(?!00)..$/ 278229/s * 0.01 + 540980/s * 0.99 = 538352.49/s 0% /^N(?!.*00$)/ 743994/s * 0.01 + 633900/s * 0.99 = 635000.94/s 18% /^N.*(?<!00$)$/ 330512/s * 0.01 + 789866/s * 0.99 = 785272.46/s 46%

    /^N.*(?<!00$)$/ is the winner as expected, and it won by no small margin (24%).