ALLInterview.com :: Home Page            
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Google
   
 
Categories >> Software >> Scripts >> CGI Perl
 
 


 

 
 PHP interview questions  PHP Interview Questions (658)
 ASP interview questions  ASP Interview Questions (251)
 CGI Perl interview questions  CGI Perl Interview Questions (76)
 JavaScript interview questions  JavaScript Interview Questions (174)
 VB Script interview questions  VB Script Interview Questions (338)
 JSP interview questions  JSP Interview Questions (99)
 Shell Script interview questions  Shell Script Interview Questions (158)
 Python interview questions  Python Interview Questions (5)
 Tcl interview questions  Tcl Interview Questions (19)
 Awk interview questions  Awk Interview Questions (6)
 AJAX interview questions  AJAX Interview Questions (24)
 Ruby on Rails interview questions  Ruby on Rails Interview Questions (21)
 Scripts AllOther interview questions  Scripts AllOther Interview Questions (48)
Question
Write a simple  regular expression to match an IP address, 
e-mail address, city-state-zipcode combination. 
 Question Submitted By :: Nimy
I also faced this Question!!     Rank Answer Posted By  
 
  Re: Write a simple regular expression to match an IP address, e-mail address, city-state-zipcode combination.
Answer
# 1
For IP Address

/([0-255])(\.)$1\1$1\1$1/;

For other question answer may vary depending on the
requirement.
 
Is This Answer Correct ?    21 Yes 31 No
Raghav
 
  Re: Write a simple regular expression to match an IP address, e-mail address, city-state-zipcode combination.
Answer
# 2
I hope this is the right way to match IP address

/((\d{1,3})(\.)){3}\d{1,3}/
 
Is This Answer Correct ?    12 Yes 24 No
Gopal
 
 
 
  Re: Write a simple regular expression to match an IP address, e-mail address, city-state-zipcode combination.
Answer
# 3
/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/


ip=$1;
 
Is This Answer Correct ?    25 Yes 18 No
Mukesh
 
  Re: Write a simple regular expression to match an IP address, e-mail address, city-state-zipcode combination.
Answer
# 4
/(\d+)(\.\d+){3}/) 
Is This Answer Correct ?    4 Yes 16 No
Leo
 
  Re: Write a simple regular expression to match an IP address, e-mail address, city-state-zipcode combination.
Answer
# 5
ip_pattern = r'\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' 
Is This Answer Correct ?    15 Yes 5 No
Vyvyan
 
  Re: Write a simple regular expression to match an IP address, e-mail address, city-state-zipcode combination.
Answer
# 6
(/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/ && $1 < 255 &&
$2 < 255 && $3 < 255 && $4 < 255 )
 
Is This Answer Correct ?    17 Yes 3 No
Abhishek Sagar
 
  Re: Write a simple regular expression to match an IP address, e-mail address, city-state-zipcode combination.
Answer
# 7
/(\d{3}\.){3}\d+/ 
Is This Answer Correct ?    3 Yes 6 No
Sanjiv
 
  Re: Write a simple regular expression to match an IP address, e-mail address, city-state-zipcode combination.
Answer
# 8
/^([1-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/



All those above answer are wrong...because all of them take

0.0.0.0 as valid IP address which is not the case...with computer science ...

Developed By Anshuman sengupta

Reviewed by Arnab Bose

Tested by Avishek chatterjee
 
Is This Answer Correct ?    1 Yes 7 No
Anshuman
 
  Re: Write a simple regular expression to match an IP address, e-mail address, city-state-zipcode combination.
Answer
# 9
The question is not properly framed. A regex is specific to
a language to some extent. Lets frame it more properly and
create some test cases:
1. Writing regex for Perl to identify an IP address.
2. IP address must be a "true" IP address.
3. It must not match anything more than an IP addresses.
4. Must pass the following tests:
a)0.0.0.0: Pass: This is a valid class A address, though a
reserved address. You may not see it often but nevertheless
it is a valid address. See
http://www.lincoln.edu/math/rmyrick/ComputerNetworks/InetRef
erence/26.htm
b)1.1.1.1: Pass
c)255.255.255.255:Pass
d)1.1.1.11111:Fail
e)256.1.1.1:Fail
f)256.256.256.256:Fail
g)-1.-1.-1.-1: Fail
h).... (four dots without any digits in between)

Another test can be presence of extra zeros, which can be
fine or not fine depending upon the OS. For example
01.01.01.01 should or should not be accepted? But we will
ignore it for the time being in our analysis.

Here is an analysis of the solutions given above as well as
my additional solutions:
1. /([0-255])(\.)$1\1$1\1$1/; : This works for egrep and
possibly vi and other *nix tools such as emacs. This does
*not* work for Perl because [0-255] will match
digits '0','1', '2' and '5' only. It will not match 63.

2. /((\d{1,3})(\.)){3}\d{1,3}/: Written for Perl as
obviously {} syntax is not available for many other
scripting languages. This does not check the semantics of
IP addresses. Fails tests d, e, f above.

3. (/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/ && $1 <
255 && $2 < 255 && $3 < 255 && $4 < 255 ): Is almost
correct. Only fails d in above tests. However, it uses
logical expressions and is not a pure regular expression.
Checking for word boundaries will correct it. For example,
(/\b(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\b/ && $1 <
255 && $2 < 255 && $3 < 255 && $4 < 255 ) will pass d
above.

4. r'\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': Passes all the
tests. This is the best answer.

5. /^([1-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]
\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25
[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/: Fails
test a. But only because it was meant to eliminate 0.0.0.0.
Otherwise this is okay, though a little verbose. #4 above
can be modified slightly to take care of 0.0.0.0 case and
is less verbose.

Hope this helps.

Regards,
Santosh
 
Is This Answer Correct ?    6 Yes 1 No
Santosh
 
  Re: Write a simple regular expression to match an IP address, e-mail address, city-state-zipcode combination.
Answer
# 10
sub validate_ip {

my $ip = shift;

if ( $ip =~ m/^([1-9]|1[0-9]{1,2}|2[0-4]\d|25[0-5])(\.([0-9]|1[0-9]{1,2}|2[0-4]\d|25[0-5])){3}$/ ) {
return 0;
} else {
return 1;
}
}

validate_ip("127.0.0.1");
 
Is This Answer Correct ?    4 Yes 3 No
Jayakumar.b
 

 
 
 
Other CGI Perl Interview Questions
 
  Question Asked @ Answers
 
How do you debug a Perl scripting ( at the compile time error or run time error) in Unix environment ?   4
how to find a substring in a string without using substr built in functions, and print the substring found Nokia 2
my @array=('data1','data2'); my @array1=('data1','data2'); my ($i,$k); $i=7; $k=7; while($i){ $array [++$#array] = 'ree'; $i--; print "@array"; } while($k){ push(@array1,'ree'); $k--; print "@array1"; } Are these two while loop are doing the same functionality ? What may be the difference?   2
What is the Common Gateway Interface?   2
Write an expression or perl script to identify the entered ip address is valid or not? HCL 5
what is the meaning of rigging?   1
write a script to generate n prime no.s? Persistent 2
Write a script to reverse a string without using Perl's built in function Nokia 8
write a perl script to find whether a given line of text is starting and ending with same word or not ??? CSS 2
what r the different type of function in perl ??? Symphony 1
What?s your favorite module and why?   3
Difference between Perl and Mod_perl?   2
 
For more CGI Perl Interview Questions Click Here 
 
 
 
 
 


   
Copyright Policy  |  Terms of Service  |  Articles  |  Site Map  |  RSS Site Map  |  Contact Us
   
Copyright 2013  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  KalAajKal.com