Although CPAN has well-documented and tested modules for mapping and checking IP addresses, there should be no good reason for you. Personally, I never had a real reason to use them for verification purposes, since I trusted / served the input.
Here is a shorter version of your regexp with your own traps:
while (my $ip = <DATA>) { chomp $ip; # older version # if($ip =~ /(\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3}\:\d{1-5})/) # see below for explanation if ($ip =~ /\b(\d{1,3}(?:\.\d{1,3}){3}:\d{1,5})\b/) { print "$ip - matches\n"; } else { print "$ip - does not match\n"; } } __DATA__ 216.108.225.236:60099 4.2.2.1:1 216.108.225.236:0 1216.1108.1225.1236:1234 216.108.225.236x:123 9216.108.225.236:8472 10.10.10.10
Results:
216.108.225.236:60099 - matches 4.2.2.1:1 - matches 216.108.225.236:0 - matches 1216.1108.1225.1236:1234 - does not match 216.108.225.236x:123 - does not match 9216.108.225.236:8472 - does not match 10.10.10.10 - does not match
Explanation:
/\b
Hope this helps.
Ashish kumar
source share