McDarren has asked for the wisdom of the Perl Monks concerning the following question:
This is just something that came up in a casual email exchange today. And I've no particular reason for posing the question here other than curiosity, and perhaps providing the opportunity for those with plenty of regex fu to show their stuff :)
"How would you construct a regex to match an integer between 0 and 100?"
My regex fu is only average at best, but after a couple of minutes I came up with:
Which gives:perl -le 'for (qw/0 1 19 32.4 100 77 138 342.1/) { print "$_ is ", /^( +?:100|\d\d?)$/ ? "valid input" : "invalid input"}'
0 is valid input 1 is valid input 19 is valid input 32.4 is invalid input 100 is valid input 77 is valid input 138 is invalid input 342.1 is invalid input
Solves the "problem", but hard-coding the 100 in the pattern is pretty horrible, I think.
So how would others do this?
Cheers,
Darren
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Matching an integer between 0 and 100
by ikegami (Patriarch) on Sep 01, 2009 at 16:42 UTC | |
by McDarren (Abbot) on Sep 01, 2009 at 17:38 UTC | |
|
Re: Matching an integer between 0 and 100
by JavaFan (Canon) on Sep 01, 2009 at 15:49 UTC | |
|
Re: Matching an integer between 0 and 100
by Anonymous Monk on Sep 01, 2009 at 17:50 UTC |