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
How will you open a file in a write-only mode in perl?
How can you replace the characters from a string and save the number of replacements?
What is a chop() function in perl?
What is the use of "stderr()"?
Explain tk?
There are some duplicate entries in an array and you want to remove them. How would you do that?
How do you turn on the perl warnings?
How to close a directory in perl?
Explain '->' in perl?
What is the use of -w, -t and strict in Perl?
What is hash?
How do find the length of an array?
How to add elements in a hash in perl?
How do you set environment variables in perl?
What happens in dereferencing?