The following regex:

/(^gi.*)([1-8])(\/)(0)(\/)([1-9]|[1-3][0-9]|4[0-8])/i

aside from not anchoring to the end of the string, as hippo has pointed out, explicitely attempts to match numbers from 1 to 48 only, so using this to extract said number and then checking to see whether it's greater than 48 won't work. It won't be; if it were the regex wouldn't match in the first place.

As an aside, is there a reason you're explicitely capturing the backslashes and the zero? I'm assuming not; I've removed those captures below, but if you need them after all for some reason just put them back in.

Now, asides aside, since you don't want to explicitely check whether the number's greater than 48, you'll just want to match the regex against the user's input, and only proceed if it does (knowing then that the number does not exceed 48). And I'd do that anyway, since if the input you're matching against is supplied by the user, the rest of it may also not be what you expect, and explicit matching will make sure you're getting precisely what you expect.

So just do something like this:

# ... if($input =~ m/^(gi.*)([1-8])\/0\/([1-9]|[1-3][0-9]|4[0-8])$/i { my ($string, $digit, $number) = ($1, $2, $3); # e.g. "GigabitEther +net", "1" and "48"; $number will be at most 48 # ... }

In reply to Re: Regex : Limit value of a string by AppleFritter
in thread [SOLVED] Regex : Limit value of a string by PtitePomme

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.