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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.