You're correct in believing that your regex says match one to three digits... but more precisely, it says, match one to three digits before a dot... and if the suspect value you feed it has something before the three-digits-before-a-dot it will match that value, too, ignoring, as does your regex, the number(s) (or letters, symbols, etc) which precede the three-digits-before-a-dot.

Illustrating anchors (where ^ means start of string (to the regex) and $ marks end of string:

# 1148750 print "Testing IP Addresses in array, \@ip: \n"; my @ip = ("0123.456.789.654", "a123.456.789.654", "123.456.789.6543", "111.222.333.444") ; for $ip (@ip) { if ( $ip =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/) { print "yes $ip has a match: $1 $2 $3 $4\n"; } else { print "no match\n"; } } print "\n\t whereas, with anchors in the ip, \n"; for $ip(@ip) { if ( $ip =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) { print "Second regex matches $1 $2 $3 $4\n"; } else { print "no match in $ip \n"; } } =head execution results: Testing IP Addresses in array, @ip: yes 0123.456.789.654 has a match: 123 456 789 654 yes a123.456.789.654 has a match: 123 456 789 654 yes 123.456.789.6543 has a match: 123 456 789 654 yes 111.222.333.444 has a match: 111 222 333 444 whereas, with anchors in the ip, no match in 0123.456.789.654 no match in a123.456.789.654 no match in 123.456.789.6543 Second regex matches 111 222 333 444 =cut

Two Updates in para 1: s/n/in/ and edited for clarity my explanation of what happens when no anchor is specified.


In reply to Re: Validate Ip address Regexp by ww
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.