Regular expression is the wrong approach IMHO.
Well, quite possibly, but the OP wanted to practice regular expressions (which is why I gave a pure regex solution) and, besides, using split is in fact using regular expressions.

But I agree that checking for numbers in the 0-255 range with a pure regex is somewhat unwieldy. An easier way might be something along these lines:

print "Valid IP\n" if 4 == grep { /^\d+$/ and $_ < 256 } split /\./, $ +ip;
(although this is still not entirely satisfactory, since this would validate something like "23.45.aa3.234.244"", so a bit more effort might be needed).

Note that Perl 6's regexes allow a code assertion to be inserted within a regex, leading for example to something like this:

my regex octet {(\d ** 1..3) <?{0 <= $0 <= 255 }> } my regex ip {^ <octet> ** 4 % '.' $}

In reply to Re^2: Validate Ip address Regexp by Laurent_R
in thread Validate Ip address Regexp by akr8986

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.