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)

Questions by ashwinraot

Showing Answers 1 - 24 of 24 Answers

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.  

  Was this answer useful?  Yes

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.  

  Was this answer useful?  Yes

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.  

  Was this answer useful?  Yes

Srikanth

  • Sep 2nd, 2013
 

Answer to first question

172.125.(2[0-5]|1[0-9]|[1-9]).0

  Was this answer useful?  Yes

Rudra

  • Sep 9th, 2013
 

In generic we can validate the IP address as
d{1,3}.d{1,3}.d{1,3}.d{1,3}

  Was this answer useful?  Yes

Sachin

  • Sep 17th, 2013
 

This worked perfectly for me to find the range between xxx.xxx.0.x and xxx.xxx.25.25

#!/usr/bin/perl
$_ =;

if($_=~ /172.125.(2[0-5]|1[0-9]|[1-9]).(2[0-5]|1[0-9]|[0-9])/)
{
print $&;
print "Match";

}
else{
print "not match";

}

  Was this answer useful?  Yes

praveen

  • Nov 29th, 2016
 

$ip=;
chomp($ip);
if ($ip =~ /^172.125.([1-9]|1[1-9]|2[1-5]).0/){print "$ip in range
" ;}
else{print "$ip not in range
"};

  Was this answer useful?  Yes

josepla

  • Jan 11th, 2017
 

Using regex for all 3 questions IP, Name and Email

Code
  1. use v5.24;

  2. use utf8;

  3. use open :std, :encoding(cp437);  # Encoding used by console, to check console type in Windows cmd use chcp

  4. use strict;

  5. use warnings;

  6.  

  7. my $answer = false;

  8. my $ip = 0.0.0.0;

  9. my $name = undef;

  10. my $email = undef;

  11.  

  12. say 1) Input ip address. Check if it is in the range 172.125.1.0 and 172.125.25.0, use only regex;

  13. chomp($ip = <STDIN>);

  14.  

  15. if ($ip =~ /^172.125.(1[0-9]|2[0-5]|[0-9]).(25[0-5]|2[0-4][0-9]|1[0-9]{1,2}|[0-9])$/) {

  16.         $answer = IP is in the range;

  17. }else{

  18.         $answer = IP out of range;

  19. }

  20.  

  21. say $answer;

  22.  

  23. $answer = false;

  24.  

  25. say 2) Input a name . Check if it is valid at least 3 letters and one vowel, with regex.;

  26. chomp($name = <STDIN>);

  27.  

  28. if ($name =~ /^[a-z]{3,}$/ & $name =~ /[aeiou]/) {

  29.         $answer = Nice name;

  30. }else{

  31.         $answer = That is not a known name;

  32. }

  33.  

  34. say $answer;

  35.  

  36. $answer = false;

  37.  

  38. sub checkEmail {

  39.         my $isemail = 0;

  40.         $isemail = ($email =~ /^.+@[^.].*.[a-z]{2,}$/);

  41. #       say $isemail;

  42.         return $isemail;

  43. }

  44.  

  45. chomp($email = <STDIN>);

  46. say 3) Input email address. Check if it is valid using return function...;

  47. $answer = checkEmail($email) ? Good email : Not valid email;

  48.  

  49. say $answer;

  50.  

  Was this answer useful?  Yes

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions