Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Matching an integer between 0 and 100

by ikegami (Patriarch)
on Sep 01, 2009 at 16:42 UTC ( [id://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.

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://792700]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found