PERL - Simple PERL scripts to do the following: Write the following regular expr
ID: 3808763 • Letter: P
Question
PERL - Simple PERL scripts to do the following:
Write the following regular expressions. Test them with a script that reads a line from STDIN and prints "yes" if it matches and "no" if not.
1. Match a name containing a capital letter followed by three lower case letters
2. Match a line that contains in it at least 3 - 15 characters between quotes (without another quote inside the quotes).
3. Replace every digit in a line of text (containing digits) with a #, and print the result.
4. Replace any number of white space charactres (new-line, tab or space) by a single space.
5. Remove all appearances of "is" from a line of text (both lowercase and uppercase letters), and print it.
Explanation / Answer
Code:
#!/usr/bin/perl
print "Enter line ";
$a=<STDIN>;
print "$a";
#matching for 1 Upper case followed by 3 lower cases alphabets
if( $a =~ / [A-Z][a-z][a-z][a-z]/ ) {
print "Matched ";
}
#Replacing digits with a #
$a =~ s/([0-9]+)/#/g;
print $a;
#replacing multiple white spaces with single space
$a =~ s/ +/ /g;
print $a;
#removing all appearances of is
$a =~ s/ is / /g;
print $a;
Output:
Enter line
this is a 1 to 1 mapping in Newyork file
this is a 1 to 1 mapping in Newyork file
Matched //1 Upper 3 lower
//replacing digits with #
this is a # to # mapping in Newyork file
//shrinking white spaces
this is a # to # mapping in Newyork file
//removing is
this a # to # mapping in Newyork file
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.