Besides the beginning and end of string anchors missing in your regex (as pointed out by ww and possibly other monks above), which explains why your regex matched something like "1000.3.4.5", there is an additional deeper problem in your regex. The digit groups of an IPv4 address have to be octets, i.e. numbers between 0 and 255 in decimal notation.

Your regex would validate an address such as 258.344.543.877, whereas none of the digit groups fits the definition of an IP address, as they are all larger than 255.

How do we check that the numbers are correct?

One possible way is to start by creating an $octet regex, which will match only numbers between 0 and 255. It could be something like this:

my $octet = qr/\d{1,2}|[01]\d{2}|2[0-4]\d|25[0-5]/;
which basically means: 1 or 2 digits (0-99), OR a 0 or 1 followed by 2 digits (000-199), OR a 2 followed by a digit between 0 and 4 and any other digit (200-249), OR a 25 followed by a digit between 0 and 5 (250-255).

Once you have defined such an $octet regex, you can use it to further define an $ip regex:

my $ip = qr/^$octet\.$octet\.$octet\.$octet$/;
in which you may want to factor out the repetition of $octet with something like this:
my $ip = qr/^(?:$octet\.){3}$octet$/;
which is, at best, only marginally clearer.

Provided I did not make any small silly mistake in the code above (I only tested a few obvious cases), this should really be able to validate any valid IPv4 address (and hopefully reject any invalid one).

Please note that I am providing the above only because you stated that you wanted to try on your own, and also because learning to build a regex from a sub-regex is sometimes really useful and may give you an initial idea of what Perl 6's or other regex-based grammars are about.

For a real life Perl 5 application, I would agree with other monks above, really recommend not to try to reinvent the wheel and advise you to use the proper ready-made module (such as Regexp::Common::net), which has undoubtedly been more thoroughly tested than my quick attempt above.


In reply to Re: 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.