P is for Practical 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.

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
heh... yes. The question is wrong. It should be "from" "to".
I'll blame the guy that originally wrote it ;)

Create A New User
Node Status?
node history
Node Type: note [id://792700]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2017-11-19 20:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (282 votes). Check out past polls.

Notices?