Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Matching string containing values between 0 and 100 inclusive

by grinder (Bishop)
on Mar 18, 2009 at 08:41 UTC ( [id://751396]=note: print w/replies, xml ) Need Help??


in reply to Matching string containing values between 0 and 100 inclusive

perl -MRegexp::Assemble -le 'print Regexp::Assemble->new->add(0..100)->as_string'

...produces...

(?:1(?:[123456789]|0?0)?|2\d?|3\d?|4\d?|5\d?|6\d?|7\d?|8\d?|9\d?|0)

This could be simplified by gathering the 2\d?, 3\d?... subpatterns and factoring them into [2-9]\d?, but that would be less efficient since character classes are slower, and in 5.10 this would bypass demerphq's trie optimisation.

On the plus side, while it will examine all the 10 primary alternatives during a fail, it's failing on EXACT matches, which is fast.

This lesson was brought to you by Stupid Regexp Trick I Can Play.

• another intruder with the mooring in the heart of the Perl

Replies are listed 'Best First'.
Re^2: Matching string containing values between 0 and 100 inclusive
by ikegami (Patriarch) on Mar 18, 2009 at 13:46 UTC

    You should use Regexp::List from the same for distro when you have strings rather than patterns. It doesn't affect the outcome in this case, but let's not get into the habit of using the wrong tool.

    perl -MRegexp::List -le'print Regexp::List->new->list2re(0..100)'

    in 5.10 this would bypass demerphq's trie optimisation.

    In 5.10, the equivalent of Regexp::Assemble would be:

    my $re = join '|', 0..100;

    and the equivalent of Regexp::List would be:

    my $re = join '|', map quotemeta, 0..100;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://751396]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-03-19 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found