I read that you're trying to validate that you've received a two-digit number, less than or equal to ten. That means to me you're looking for 01, 02, 03, 04, 05, 06, 07, 08, 09, and 10, and that you want to REJECT 1, 2, 3, 4, 5, 6, 7, 8 and 9. In other words, it appears by the way you've worded your question, and by the way that you've constructed your regular expression, that the leading '0' is significant to you.
Perl, on the other hand, is happy to convert strings to numbers (if it can), and numbers to strings. That's helpful sometimes, and other times it might just get in your way. Here's the issue: Let's say you start with a string, "01", and then you perform some operation on it whence Perl must treat that string as a number. That's fine, but once it's been treated as a number, Perl no longer cares that it at one time had a leading zero. This is because, numerically, 01 is the same as 1, and thus, once treated as a number, that leading zero loses significance and *poof*, it's just gone.
Now, we don't see in your example code where $totalNumber comes from. And we don't see how you've treated it within your script. My suspician is that though you may at one point have had yourself a nice "01" string, you performed some operation that resulted in the string being taken as a number, so the leading '0' disappears.
If, on the other hand, you don't care about that leading '0', your regexp should probably just be written like this:
m/^\d{1,2}$/Also, the text your script prints errantly reports that with an input of 10 (ten), "the number is less than 10". It should say, "the number is less than or equal to ten\n".
Dave
In reply to Re: vallidating a regular expression
by davido
in thread vallidating a regular expression
by s_gaurav1091
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |