Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Matching an integer between 0 and 100

by ikegami (Pope)
on Sep 01, 2009 at 16:42 UTC ( #792700=note: print w/ replies, xml ) Need Help??


in reply to Matching an integer between 0 and 100

Your pattern matches 0 and 100 even though neither are between 0 and 100. Is your question wrong or is your solution wrong?

Solutions for "integers between 0 and 100":

my $pat = qr/ ([0-9]+) (?(?{ my $n = $^N; $n <= 0 || $n >= 100 })(?!)) /x;
my $pat = qr/0|[1-9][0-9]?/;
my ($pat) = map qr/$_/, join '|', #map quotemeta, 1..99;
use Regexp::List qw( ); my $pat = Regexp::List->new()->list2re( 1..99 );

Solutions for "integers from 0 to 100":

my $pat = qr/ ([0-9]+) (?(?{ my $n = $^N; $n < 0 || $n > 100 })(?!)) /x;
my $pat = qr/0|100|[1-9][0-9]?/;
my ($pat) = map qr/$_/, join '|', #map quotemeta, 0..100;
use Regexp::List qw( ); my $pat = Regexp::List->new()->list2re( 0..100 );

In all cases, you'll need to anchor the pattern as appropriate.

The first solution of each set accepts leading zeroes. Easily changed.
The bottom three solutions of each set don't accept leading zeroes. Easily changed.

Update: Oops. Accidentally omitted a "?" from my patterns.


Comment on Re: Matching an integer between 0 and 100
Select or Download Code
Re^2: Matching an integer between 0 and 100
by McDarren (Abbot) on Sep 01, 2009 at 17:38 UTC
    "Is your question wrong or is your solution wrong?"
    heh... yes. The question is wrong. It should be "from" "to".
    I'll blame the guy that originally wrote it ;)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-09-23 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls