In Chapter 6: Regular Expressions Basics, we explore Perl’s powerful tools for pattern matching, substitution, and transliteration. These skills are essential for efficient text processing and data manipulation. Here are 30 multiple-choice questions (MCQs) divided into topics to test your knowledge on Regular Expressions in Perl.
Introduction to Regex in Perl
Which operator is used to perform a regex match in Perl?
A) :=
B) !~
C) =~
D) ==
What is the purpose of regular expressions in Perl?
A) To perform arithmetic calculations
B) To handle input/output operations
C) To match patterns in strings
D) To define subroutines
What is the correct syntax to search for the pattern “abc” in a string using Perl?
A) /abc/
B) abc/
C) “abc”/
D) /”abc”/
In Perl, what does the =~ operator indicate?
A) Assignment of a pattern to a string
B) Matching a regular expression
C) Replacing a substring
D) Comparing two strings
Which of the following is the syntax to check if a string matches a regular expression in Perl?
A) $string =~ /pattern/;
B) $string =~ /pattern/;
C) /string/ =~ pattern;
D) pattern =~ $string;
What does the !~ operator do in Perl regular expressions?
A) It performs a match
B) It checks if the string matches the pattern
C) It checks if the string does not match the pattern
D) It replaces a match
How do you escape a special character in a regular expression in Perl?
A) By enclosing it in parentheses
B) By placing a backslash before the character
C) By using quotes around the character
D) By doubling the character
Which of the following is used to match the beginning of a string in a regular expression?
A) ^
B) $
C) *
D) +
What does the $ symbol represent in a regular expression in Perl?
A) The end of the string
B) A special character
C) The start of a string
D) A space character
Which metacharacter is used to match any single character except a newline?
A) .
B) *
C) +
D) ?
Simple Match (=, !)
What will the following code return in Perl? if (“hello” =~ /ell/) { print “Match\n”; } A) “Match” B) “hello” C) Error D) Nothing
What is the output of the following Perl statement? if (“hello” !~ /world/) { print “No match\n”; } A) No match B) world C) Error D) Match
Which of the following regex patterns will match the word “perl” in the string “I love perl”? A) /perl/ B) perl/ C) “perl” D) /”perl”/
What will be the result of the following Perl expression? if (“apple” =~ /p/) { print “Found\n”; } A) Found B) apple C) Error D) No match
What does the following code check for? if ($string =~ /abc/) { … } A) If the string contains “abc” B) If the string starts with “abc” C) If the string ends with “abc” D) If the string is exactly “abc”
How can you check if a string does not contain the pattern “123” in Perl? A) $string =~ /123/; B) $string !~ /123/; C) $string eq /123/; D) $string == /123/;
In Perl, what does the expression $str =~ /pattern/ return if the match is successful?
A) The string
B) True
C) False
D) The pattern
Which of the following code snippets checks if the string $text starts with “abc”?
A) if ($text =~ /^abc/)
B) if ($text =~ /abc$/)
C) if ($text =~ abc^)
D) if ($text =~ ^abc)
How do you match a pattern only if it does not occur in a string in Perl?
A) Use the !~ operator
B) Use the =~ operator
C) Use if
D) Use match
What is the purpose of using the =~ operator in regular expressions?
A) To replace text
B) To compare strings
C) To check for a match between a string and a pattern
D) To delete parts of a string
Substitution and Transliteration
Which operator is used for substitution in Perl regular expressions?
A) s///
B) //
C) m///
D) tr///
What is the correct syntax for replacing all occurrences of “apple” with “orange” in a string?
A) $string =~ s/apple/orange/g;
B) $string =~ s/apple -> orange/g;
C) $string = s/apple/orange/g;
D) $string = s/apple -> orange/g;
How can you substitute only the first occurrence of “apple” with “orange” in Perl?
A) $string =~ s/apple/orange/;
B) $string =~ s/apple/orange/g;
C) $string = s/apple/orange/;
D) $string = s/apple/orange/g;
What does the g modifier do in a substitution?
A) It replaces only the first occurrence
B) It replaces all occurrences
C) It escapes special characters
D) It matches the pattern globally
Which function is used for transliteration in Perl?
A) tr///
B) s///
C) m///
D) g///
What is the output of the following Perl code? my $str = “abc”; $str =~ tr/a/b/; print $str; A) bbc B) abc C) ccc D) aac
How can you use transliteration to change every “a” to “b” in a string? A) $string =~ tr/a/b/; B) $string = tr/a/b/; C) $string =~ s/a/b/; D) $string =~ m/a/b/;
What will the following code do? my $text = “apple”; $text =~ s/a/A/g; print $text; A) Apple B) aApple C) ApAp D) apple
How do you apply a regular expression with substitution to replace “cat” with “dog”?
A) $str =~ s/cat/dog/;
B) $str =~ tr/cat/dog/;
C) $str = s/cat/dog/;
D) $str = s/cat/dog/g;
Which of the following is a valid regular expression for matching “hello” in a string?
A) /hello/
B) hello/
C) “hello”/
D) hello@
Answer Key
Qno
Answer
1
C) =~
2
C) To match patterns in strings
3
A) /abc/
4
B) Matching a regular expression
5
A) $string =~ /pattern/;
6
C) It checks if the string does not match the pattern
7
B) By placing a backslash before the character
8
A) ^
9
A) The end of the string
10
A) .
11
A) “Match”
12
A) No match
13
A) /perl/
14
A) Found
15
A) If the string contains “abc”
16
B) $string !~ /123/;
17
B) True
18
A) if ($text =~ /^abc/)
19
A) Use the !~ operator
20
C) To check for a match between a string and a pattern