Write an expression or perl script to identify the entered
ip address is valid or not?

Answers were Sorted based on User's Feedback



Write an expression or perl script to identify the entered ip address is valid or not?..

Answer / savitha sridhar

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<=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 ?    12 Yes 1 No

Write an expression or perl script to identify the entered ip address is valid or not?..

Answer / 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

Write an expression or perl script to identify the entered ip address is valid or not?..

Answer / digesh patel

if($ipaddress=~m\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)
print "\n It is a valid IP address :";

Is This Answer Correct ?    0 Yes 0 No

Write an expression or perl script to identify the entered ip address is valid or not?..

Answer / subhash chandran

if($IP !~ /(((\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(\d|
[1-9]\d|1\d{2}|2[0-4]\d|25[0-5]))/)
{
print "Valid\n";
}

Is This Answer Correct ?    0 Yes 0 No

Write an expression or perl script to identify the entered ip address is valid or not?..

Answer / snehal kashikar

#! /usr/bin/per
use strict;
#- Use Input
#---------------
my $ip_add = "";

#- Script start
#---------------
print"Enter the IP address\n";
$ip_add = <STDIN>;
chomp($ip_add); # Remove the last "\n" character
$ip_add=$ip_add."\."; # Append . at the end of IP address
#-------Expression to check IP address is valid or not-----#
if($ip_add =~ m/^[0-2]?[0-5]?[0-5]?\.){4}$){
print" Valid IP Address\n";
}else{
print" Invalid IP Address\n"
}

Is This Answer Correct ?    1 Yes 7 No

Post New Answer

More CGI Perl Interview Questions

Write a script to reverse a string without using Perl's built in function

8 Answers   Nokia,


How to know whether a key exists or not in perl?

0 Answers  


What are the purpose of close(), getc() and read() functions?

0 Answers  


What is an interpolation in perl?

0 Answers  


How to remove a directory in perl?

0 Answers  






Explain what is perl language?

0 Answers  


Consider the following example #! /bin/perl use strict; sub sample { my @arr=(1,2,3,4); return @arr; } my ($a,$b,$c,$d) = &sample; print "$a\n$b\n$c\n$d\n"; In the above code, How can I get the $c without using the arguments such as $a,$b. I don't want to use any array to get the return values.

2 Answers  


What is the Common Gateway Interface?

2 Answers  


How the interpreter is used in Perl?

0 Answers  


Explain the default scope of variables in perl?

0 Answers  


What is perl push array function?

0 Answers  


Distinguish my and local?

0 Answers  


Categories