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

Explain about typeglobs?

555


Explain the difference between "my" and "local" variable scope declarations. ?

563


What interface used in PERL to connect to database? How do you connect to database in Perl?

535


you are required to replace a char in a string and store the number of replacements. How would you do that?

517


What is subroutine in perl?

533






What is the use of -w, -t and strict in Perl?

567


Explain substr function in perl?

540


What is the use of -n and -p options?

604


Explain join function in perl?

537


How to determine strings length in perl?

539


Which functions in perl allows you to include a module file. State their differences.

530


How can we create perl programs in unix, windows nt, macintosh and os/2 ?

485


What is the use of command “use strict”?

566


What does delete function do in perl?

521


how to connect cisco switch uisng perl script

3385