http://www.perlmonks.org?node_id=792700


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 ;)