Gerard's Perl Page
Index
Home
About
Old News
Contribute



Portfolio
metamail
rifle
ubh
whatpix
ydecode

Links
use Perl;
Perl Monks
www.perl.com
ActiveState
CPAN
Perl User Groups
Perl Mongers
Powered By Perl
Template Toolkit
Emacs

Validating An IP Address
by gerard on Mon Dec 10 00:00:00 PST 2001

Here is a short snippet of code which shows you how to validate an IP address using Regexp::Common.


#!/usr/bin/perl -w



use strict;



use Regexp::Common;



while (<DATA>) {

    print '-' x 30, "\n";

    chomp;

    foreach my $i (qw( dec hex oct bin)) {

        if(/(?i)$RE{net}{IPv4}{$i}{-keep}/) {

            print "$_ matches $i, $1\n";

        }

    }

}



__DATA__

255.255.255.255

255.255.255.0

0.0.0.0

127.0.0.1

192.168.0.1

192.168.0.254

11.22.33.44

aa.bb.cc.dd

c7.ae.04.ad

199.174.4.173

C7.AE.04.AD