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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |