GeekInterview.com
Series: Subject: Topic:
Question: 1 of 65

Perl Code

1) Input ip address. Check if it is in the range 172.125.1.0 and 172.125.25.0 using only regular expressions in Perl. Do not use parse functions. 2) Input a name. Check if it is valid (has at-least 3 letters and one vowel) using only return functions and regular expressions in Perl. Do not use parse functions. 3) Input email address. Check if it is valid using return function and regular expressions in Perl. Do not use parse functions. 4) Input a number. Convert the number into words using return function and regular expressions in Perl. Do not use parse functions.(15 => one five)
Asked by: ashwinraot | Member Since Aug-2012 | Asked on: Aug 29th, 2012

View all questions by ashwinraot

Showing Answers 1 - 3 of 3 Answers
Gaurav.khurana

Answered On : Mar 11th, 2013

View all answers by Gaurav.khurana

Answer to question 1 #logic we are retrieving the value after the second (.) and third (.) and checking if they are in the range or not # $1 = first value match in the pattern match which is there in first parenthesis (d)+ # $2 = second value match in the pattern match which is there in second parenthesis (d)+

Code
  1.  
  2. /home/gkhurana> perl
  3.  
  4. chomp($ip=<STDIN>);
  5. if($ip =~ m/172.125.(d+).(d+)/)
  6. {
  7.    if ($1 >=1 and $1 <=25 and $2 >=0 and $2 <=255 )
  8.         {
  9.         print "
  10. IP address is within the range
  11. " ;
  12.         }
  13.         else
  14.         {
  15.         print "
  16. IP address is out of the range
  17. ";
  18.         }
  19. }
  20. 172.125.1.20
  21.  
  22. IP address is within the range
  23.  
  24. /home/gkhurana>
  25.  

  
Login to rate this answer.
Gaurav.khurana

Answered On : Mar 11th, 2013

View all answers by Gaurav.khurana

First we are searching for a vowel , if its found we are removing it from the string example if input string $in=hello, then since vowels are present in it , we are removing them so $in=hll (removed all vowels) , now we know there were vowels present in the input now we are making sure that rest string should have atleast 3 non vowel characters

Code
  1. print "Please enter the input(0 to exit) :- ";
  2. chomp($in=<STDIN>);
  3. if ($in=~ s/[aeiou]+//gi and $in=~/[b-df-hj-np-tv-z]{3,}/ig)
  4. {
  5.  print "
  6. It contains a vowel and atleast 3 characters which are not vowels .
  7. ";
  8. }
  9. else
  10. {
  11. It does not meet the requirements
  12. ";
  13. }
  14.  

  
Login to rate this answer.
Gaurav.khurana

Answered On : Mar 11th, 2013

View all answers by Gaurav.khurana

answer to question 3: Validated the general email id that we use but not all validation as suggested on http://en.wikipedia.org/wiki/Email_address

Code
  1. /home/gkhurana> perl
  2.  
  3. @input=(a@gmail.com,a@a.ca,bcfdd.com,abc@a.in,aaaaaaaaaaaaaaaaa@a.,abcasbcscdb@A.com,abc1234@yahoo.com,abc1234abc@yahoo.co.in,1234@yahoo.com,sachin.te@yahoo.com);
  4. foreach $in (@input)
  5. {
  6. # first matching should start with character then . or _ and then numbers and then @ symbol after that yahoo.com or it could be yahoo.co.in
  7. # assuming that atleast 2 character should come after .
  8.  
  9. if ($in=~/^ [a-z]+ .?  \_?   [0-9]*   [a-z]*   @   [a-z]+  .[a-z]{2,} .?([a-z]{2,})?$  /ix)
  10. {
  11.  print "
  12. $in  is a correct email id
  13. ";
  14. }
  15. else
  16. {
  17.  print "
  18. $in  is not a correct email id
  19. ";
  20. }
  21. }
  22. ^D
  23. a@gmail.com  is a correct email id
  24.  
  25. a@a.ca  is a correct email id
  26.  
  27. bcfdd.com  is not a correct email id
  28.  
  29. abc@a.in  is a correct email id
  30.  
  31. aaaaaaaaaaaaaaaaa@a.  is not a correct email id
  32.  
  33. abcasbcscdb@A.com  is a correct email id
  34.  
  35. abc1234@yahoo.com  is a correct email id
  36.  
  37. abc1234abc@yahoo.co.in  is a correct email id
  38.  
  39. 1234@yahoo.com  is not a correct email id
  40.  
  41. sachin.te@yahoo.com  is a correct email id
  42.  
  43. /home/gkhurana>
  44.  

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.