Write an expression or perl script to identify the entered
ip address is valid or not?

Answer Posted / anil

The first reply won't match 192.168.99.99. [0-5] matches
only 0,1,2,3,4,5. How about the other numbers?

Second one is almost correct, but the first octet can't be
'0'. I mean, 0.1.2.3 is not a valid address.

print "Enter an ip address: ";
$ans=<stdin>;
chomp($ans);
if ($ans =~ m/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
{
if ( ($1>0) && ($1<=255) && ($2<=255) && ($3<=255) &&
($4<=255))
{
print "An IP Address";
}
else
{
print "Not an IP Address";
}
}
else
{
print "Not an IP Address";
}

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Which of these is a difference between Perl and C++ ?

582


What is goto statement in perl?

522


Write an example explaining the use of symbol tables.

570


What is the use of -t?

556


What is it meants by '$_'?

549






Explain subroutine in perl?

502


What is the syntax used in Perl grep function?

610


What does file test operators do in perl?

515


Explain lists and ivalue?

576


How can you use Perl warnings and what is the importance to use them?

554


What is 'rollback' command in perl?

556


How to read file into hash array ?

587


Can you add two arrays together?

539


What is the difference between single (') and double (") quote in a string in perl?

492


What is a chop() function in perl?

589