Just in case you have trouble understanding some of the examples given, I'll
through out a starter regex and you can see how the process kind of works.
my $ip = $ARGV[0];
my $badoctet = 0;
if($ip =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){
for($1,$2,$3,$4){ #right format so far...
if($_ <=255 && $_ >= 0){ #is it between 0 and 255?
print ".";
}else{
$badoctet = 1; #just so we can check later...
}
}
if($badoctet){
print "One of your octets was out of range. Bad IP.\n";
}else{
print "The IP address seems ok.\n";
}
}else{
print "I don't think that's a valid IP.\n"; #wasn't even in the right
format.
}
If you look through the regex, it is checking for four sets of one to three
digits (\d{1,3}), separated by a period. Because each set of digits is
encased in parentheses, Perl stores the result of that part of the match in
variables called $1,$2, etc., so you can later check real quick to make sure
that each set of digits is between 0 and 255. I don't recommend using this
in a production environment, mind you, because there are probably all sorts
of issues with IP addresses that I haven't even thought of, so if you're
really looking to put this into some industrial grade code, you should use
or look at the source code to a module from CPAN. I'm sure there's a bunch
for this.
-----Original Message-----
From: perl-Krackedpress [mailto:[EMAIL PROTECTED]]
Sent: Sunday, August 18, 2002 9:12 PM
To: Janek Schleicher
Cc: [EMAIL PROTECTED]
Subject: Re: how to make a regex for a ip address
I hope you have a good REF. for regex commands
O'Reilly's series book is the best I am told.
I do not have one for the IP address
but I have
email, zipcode, and phone
Here are the strings for regex
for the following
// check the email fields for validity
"^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$"
// check zipcodes for validity - but not the last 4 business digits
"(^[0-9]{5})-([0-9]{4}$)", trim($zip_code)) &&
(!ereg("^[a-zA-Z][0-9][a-zA-Z][[:space:]][0-9][a-zA-Z][0-9]$"
// check phone for validity
"(^(.*)[0-9]{3})(.*)([0-9]{3})(.*)([0-9]{4}$)"
********************************************
Timothy Lungstrom
Kracked Press Productions
[EMAIL PROTECTED]
******************************************
----- Original Message -----
From: "Janek Schleicher" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, August 18, 2002 1:24 PM
Subject: Re: how to make a regex for a ip address
> Alex Chen wrote at Sun, 18 Aug 2002 17:14:21 +0200:
>
> > i am a beginners in perl.i am studying perl regex now ,and i want to
know
> > how to make a regex for a ip address.
>
> Have a look to the
> Regexp::Common
> module from the CPAN.
>
> $RE{net}{IPv4} contains the regexp to match an ip address.
>
>
> Best Wishes,
> Janek
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]