Jamie Murray wrote: > Hi Glenn, > I have worked on this further and looked at some of the previous posts. > I have tried this with all different combinations of ip address and this has > worked. > Of course I got the idea from a previous post from Alex and Mark Thomas. > Please understand I could have just copied and pasted Mark's solution but > then I wouldn't have learned anything. > > if($num =~ /^(([0-1]{0,1}[0-9]{0,1}[0-9]|2[0-4][0-9]|25[0-5])\.?){4}$/) > > I feel that Mark's post is the better choice though and much > slicker than mine. > > my $octet = qr/\d|[01]?\d\d|2[0-4]\d|25[0-5]/; > my $valid_ip = qr/^$octet\.$octet\.$octet\.$octet$/o; > > print "yes" if $ip =~ $valid_ip;
This should cover some failing cases : use strict; my $DDD = qr{(:?\d|[01]?\d\d|2[0-4]\d|25[0-5])}; # a dotted decimal digit my @ips = ('1.254.255.255', '256.257.258.259', '127.0.0.1', '127.0.1', '127.1', '127'); foreach (@ips) { # made digits 2,3,4 optional if (/^$DDD(:?(:?(:?\.$DDD)?\.$DDD)?\.$DDD)?$/o) { print " OK: $_\n"; } else { print "BAD: $_\n"; } } __END__ This would be faster but uglier : /^(?:\d|[01]?\d\d|2[0-4]\d|25[0-5])(((\.(?:\d|[01]?\d\d|2[0-4]\d|25[0-5]))?\.(?:\d|[01]?\d\d|2[0-4]\d|25[0-5]))?\.(?:\d|[01]?\d\d|2[0-4]\d|25[0-5]))$/ Then there's hex (0x7f000001) and decimal IP addresses (2130706433) and IPV6 addresses ([::127.0.0.1]) etc, and inet_aton is still the way to go for pure correctness in checking an IP number. -- ,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED] (_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED] / ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/ -/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff) _______________________________________________ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs