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
What happens in dereferencing?
What is qq (double q)operator in perl?
How to read file into hash array ?
What is the difference between exec and system?
How do I sort a hash by the hash value?
How to change a directory in perl?
Explain socket programming in perl?
How to merge two arrays in perl?
How many loop control keywords are there in perl?
What are the various perl data types based on the context?
How to close a file in perl?
How does a “grep” function perform?
What is the easiest way to download the contents of a URL with Perl?
What is the purpose of “_file_ literal” and “_line_ literal” in perl?
Define print() function in perl?