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
Which of these is a difference between Perl and C++ ?
What is goto statement in perl?
Write an example explaining the use of symbol tables.
What is the use of -t?
What is it meants by '$_'?
Explain subroutine in perl?
What is the syntax used in Perl grep function?
What does file test operators do in perl?
Explain lists and ivalue?
How can you use Perl warnings and what is the importance to use them?
What is 'rollback' command in perl?
How to read file into hash array ?
Can you add two arrays together?
What is the difference between single (') and double (") quote in a string in perl?
What is a chop() function in perl?