in reply to Regexp for range of numbers

Of course, with 5.9.2, there is a much nicer way to construct an efficient regular expression to match the numbers from 0 to 255 without leading zeroes:

use strict; my $zero_to_255 = join "|", 0..255; $zero_to_255 = qr(^(?:$zero_to_255)$); print $zero_to_255,"\n"; for (-2,-1, 0..259) { print "$_\n" unless /$zero_to_255/; };

Replies are listed 'Best First'.
Re^2: Regexp for range of numbers
by 5mi11er (Deacon) on Apr 05, 2005 at 15:52 UTC
    So, stringing together a somewhat large number of static ORs in a regular expression is efficient?
    • Programmer wise, I agree.
    • Space wise, not so much.
    • Time wise? If it is, it is counter intuitive to me...
    -Scott

      Please note that I mentioned 5.9.2, the most current release of the developer branch, which has a patch by demerphq, that builds a very optimized tree out of a list like mine. The tree (actually a Trie) can the optimally match/compare against the string, by looking at exactly the first three chars of the string and quickly decide if it matches or not.

        Very cool. So is there detailed information available somewhere about this new feature? I imagine it's doing some hash or binary tree type searches...

        -Scott