IP Address Validation

  • + 0 comments

    perl

    use capture groups to test that IPv4 digits are valid

    while(<>){
        chomp;
        next if $. == 1;
        if (/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/){
        ($1 < 256 && $2 < 256 && $3 < 256 && $4 < 256) ? print "IPv4\n" : print "Neither\n";
            
        } elsif ( /^([a-f\d]{1,4}):([a-f\d]{1,4}):([a-f\d]{1,4}):([a-f\d]{1,4}):([a-f\d]{1,4}):([a-f\d]{1,4}):([a-f\d]{1,4}):([a-f\d]{1,4})$/ ){
            print "IPv6\n";
        } else {
            print "Neither\n";
        }