more useful options 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?
 [Corion]: A good weekstart to everybody!

How do I use this? | Other CB clients
Other Users?