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

Why is it hard to call this function: sub y { "because" } ?

529


Create a function that is only available inside the scope where it is defined ?

547


What are arrays in perl?

553


Explain perl.

549


What are perl strings?

500






What is the peculiarity of returning values by subroutines in perl?

478


What are the features of perl language?

584


What is perl I used for?

525


What are some common methods to all handles in perl?

496


How to open and read data files with Perl

601


Comment on array slicing and range operator

520


How to find the length of an array in perl?

557


What does read () command do?

517


Assuming both a local($var) and a my($var) exist, what's the difference between ${var} and ${"var"}?

535


How do I print the entire contents of an array with Perl?

520