Home: Perl Programming Help: Regular Expressions:
how to deal it



njalex
New User

Aug 18, 2002, 9:08 AM


Views: 13831
how to deal it

hi ,erveryone

i have a question,how to make a reg ex to check

a ip address is correct.for example

192;168,1.42 ->this ip address is wrong

192.168.1.42 ->this ip address is correct.

thanks !!


Paul
Enthusiast

Aug 18, 2002, 4:21 PM


Views: 13828
Re: [njalex] how to deal it

$ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;


davorg
Thaumaturge / Moderator

Aug 19, 2002, 1:14 AM


Views: 13824
Re: [RedRum] how to deal it

Of course, that also matches invalid IP addresses like 300.400.500.600 and the like.

I haven't tested it, but the one provided in Regexp::Common may be more complex (or, rather, almost unreadable!)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


(This post was edited by davorg on Aug 19, 2002, 2:48 AM)


Paul
Enthusiast

Aug 19, 2002, 2:04 AM


Views: 13823
Re: [davorg] how to deal it

Sigh. It's not that hard to come up with something.


Code
my $ip  = '111.111.111.256'; 
my @ip = split /\./, $ip;
my $big = grep $_ > 255, @ip;
my $bad = grep /\D/, @ip;

if (scalar(@ip) != 4 or $big or $bad) {
print "Invalid";
}
else {
print "OK";
}



(This post was edited by RedRum on Aug 19, 2002, 3:17 AM)


njalex
New User

Aug 20, 2002, 8:15 AM


Views: 13816
Re: [RedRum] how to deal it

what my means is if the ip address is like 123456.78

how can you check it out that this ip address is correct.

in one line reg ex,so i don't think this code meet my means.after all thanks.


(This post was edited by njalex on Aug 20, 2002, 5:22 PM)


davorg
Thaumaturge / Moderator

Aug 20, 2002, 9:46 AM


Views: 13813
Re: [njalex] how to deal it

Looks to me like any of the solutions given will work.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


thebitch
User

Aug 25, 2002, 9:42 AM


Views: 13798
Re: [njalex] how to deal it


In Reply To
what my means is if the ip address is like 123456.78

how can you check it out that this ip address is correct.

in one line reg ex,so i don't think this code meet my means.after all thanks.

Regular expressions are used to match patterns, not do math. You could do it with a regular expression, but it's not worth the trouble (for you or the regex engine).

You need to learn
http://www.perldoc.com/perl5.6.1/pod/perlsub.html