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
In reply to Matching an integer between 0 and 100 by McDarren
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |