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
Why is it hard to call this function: sub y { "because" } ?
Create a function that is only available inside the scope where it is defined ?
What are arrays in perl?
Explain perl.
What are perl strings?
What is the peculiarity of returning values by subroutines in perl?
What are the features of perl language?
What is perl I used for?
What are some common methods to all handles in perl?
How to open and read data files with Perl
Comment on array slicing and range operator
How to find the length of an array in perl?
What does read () command do?
Assuming both a local($var) and a my($var) exist, what's the difference between ${var} and ${"var"}?
How do I print the entire contents of an array with Perl?