I cant make it work ...
$a = '123.123.123';
if ($a =~ <your_regex_here> ) {
print "IP OK";
}
it gave me errors ...
Main Topics
Browse All TopicsHi,
Could you please help me with regex to check if $ip is a valid ip format?
it should be like this a.b.c.d
where a,b,c,and d between 0 - 255;
and a may not 0 :)
thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
There are modules to make this an easy task, eg:
use Net::IPv4Addr qw(:all);
my $ip="10.10.10.300";
if (ipv4_checkip($ip)) {
print "$ip is valid\n";
}
else {
print "$ip is invalid\n";
}
See http://search.cpan.org/aut
Business Accounts
Answer for Membership
by: arhumanPosted on 2002-12-09 at 11:37:55ID: 7555786
The pure regex solution is probably not the best answer here :
-5])\.([01 ]?\d\d?|2[ 0-4]\d|25[ 0-5])\.([0 1]?\d\d?|2 [0-4]\d|25 [0-5])\.([ 01]?\d\d?| 2[0-4]\d|2 5[0-5])$
0-4]\d|25[ 0-5])\.([0 1]?\d\d?|2 [0-4]\d|25 [0-5])\.([ 01]?\d\d?| 2[0-4]\d|2 5[0-5])\.( [01]?\d\d? |2[0-4]\d| 25[0-5])$
{1,3})\.(\ d{1,3})$
Friedl (Mastering regular expression)gave the :
^([01]?\d\d?|2[0-4]\d|25[0
which sadly matches : 0.0.0.0
(by the way, a legal ip address!)
^(?!0+\..*$)([01]?\d\d?|2[
is a sligtly modified version inspired Friedl again, to avoid the first zero)
For my part, I'd suggest the more simple :
^(\d{1,3})\.(\d{1,3})\.(\d
and $1>0 && $1<256
and $2>=0 && $2<256
and $3>=0 && $3<256
and $4>=0 && $4<256