CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



njalex
New User

Aug 18, 2002, 9:08 AM

Post #1 of 7 (2145 views)
how to deal it Can't Post

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

Post #2 of 7 (2142 views)
Re: [njalex] how to deal it [In reply to] Can't Post

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


davorg
Thaumaturge / Moderator

Aug 19, 2002, 1:14 AM

Post #3 of 7 (2138 views)
Re: [RedRum] how to deal it [In reply to] Can't Post

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

Post #4 of 7 (2137 views)
Re: [davorg] how to deal it [In reply to] Can't Post

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

Post #5 of 7 (2130 views)
Re: [RedRum] how to deal it [In reply to] Can't Post

what my means is if the ip address is like 12£»34£¬56.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

Post #6 of 7 (2127 views)
Re: [njalex] how to deal it [In reply to] Can't Post

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

Post #7 of 7 (2112 views)
Re: [njalex] how to deal it [In reply to] Can't Post


In Reply To
what my means is if the ip address is like 12£»34£¬56.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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives