MCQs on Advanced Regular Expressions | Perl

Enhance your Perl programming with advanced regular expressions. Master quantifiers, assertions, capturing groups, backreferences, and advanced pattern matching using modifiers to create powerful and efficient regex solutions.


Quantifiers and Assertions (10 Questions)

  1. Which quantifier matches zero or more occurrences of a pattern?
    A) +
    B) *
    C) ?
    D) {n,}
  2. What does the quantifier + mean in a regular expression?
    A) Matches zero or more occurrences
    B) Matches one or more occurrences
    C) Matches exactly one occurrence
    D) Matches a specific range of occurrences
  3. How do you specify a pattern that matches between 3 and 5 occurrences?
    A) {3,5}
    B) {3|5}
    C) {3-5}
    D) (3,5)
  4. What does the ? quantifier do when placed after a pattern?
    A) Matches zero or one occurrence
    B) Matches exactly one occurrence
    C) Matches one or more occurrences
    D) Makes the pattern optional
  5. Which assertion matches the start of a string?
    A) ^
    B) $
    C) \A
    D) \z
  6. How do you match a word boundary in Perl regex?
    A) \B
    B) \W
    C) \b
    D) \D
  7. What does the (?=...) assertion represent?
    A) Negative lookahead
    B) Positive lookahead
    C) Positive lookbehind
    D) Negative lookbehind
  8. What is the purpose of \Z in regular expressions?
    A) Matches the start of a string
    B) Matches the end of a string, ignoring a trailing newline
    C) Matches a word boundary
    D) Matches a single character
  9. How is a non-greedy quantifier specified in Perl regex?
    A) By appending ? after the quantifier
    B) By using *
    C) By using {0,1}
    D) By prefixing the quantifier with ?
  10. Which assertion checks if a pattern does not follow a specific text?
    A) (?!...)
    B) (?<=...)
    C) (?>...)
    D) (?<!...)

Capturing Groups and Backreferences (10 Questions)

  1. How is a capturing group defined in Perl?
    A) [pattern]
    B) {pattern}
    C) (pattern)
    D) <pattern>
  2. What is the purpose of $1 in a regex operation?
    A) Represents the first capturing group’s value
    B) Refers to the matched string
    C) Represents a quantifier
    D) Refers to the entire regex
  3. Which syntax refers to the second capturing group in Perl?
    A) \1
    B) \2
    C) $1
    D) $2
  4. How do you match the exact same text as the first capturing group?
    A) \1
    B) \2
    C) (?:group)
    D) $1
  5. What does (?:...) represent in Perl regex?
    A) Non-capturing group
    B) Positive lookbehind
    C) Negative lookahead
    D) Capturing group
  6. What happens if you use a capturing group within a looped pattern?
    A) It captures all matches into a single group
    B) It captures only the last match
    C) It captures each match separately
    D) It throws an error
  7. Can backreferences be used within the same regex?
    A) Yes, always
    B) No, they are only for replacement
    C) Yes, but only with non-capturing groups
    D) Yes, but only for assertions
  8. How do you refer to a named capturing group in Perl?
    A) (?<name>...)
    B) (?'name'...)
    C) Both A and B
    D) Neither A nor B
  9. Which backreference corresponds to the entire match in Perl?
    A) $0
    B) $1
    C) $&
    D) $+
  10. What is the purpose of $+ in a regex match?
    A) Refers to the last capturing group matched
    B) Matches any character
    C) Represents a quantifier
    D) Matches the start of the string

Using Modifiers and Advanced Pattern Matching (10 Questions)

  1. What does the /i modifier do in Perl regex?
    A) Enables multiline matching
    B) Makes the pattern case-insensitive
    C) Matches all occurrences
    D) Treats the pattern as binary
  2. Which modifier enables multi-line matching?
    A) /s
    B) /i
    C) /m
    D) /g
  3. What is the purpose of the /g modifier in Perl regex?
    A) Match the pattern globally
    B) Match patterns greedily
    C) Enables binary mode
    D) Disables case sensitivity
  4. How does the /x modifier affect a regex?
    A) Allows use of whitespace and comments for readability
    B) Matches all whitespace in the pattern
    C) Ignores special characters
    D) Repeats the pattern
  5. What does the /s modifier do?
    A) Makes . match newline characters
    B) Treats the string as binary
    C) Makes matching case-sensitive
    D) Matches entire strings
  6. How do you combine multiple modifiers in Perl regex?
    A) Concatenate them as /imx
    B) Separate them by commas
    C) Use parentheses around each
    D) They cannot be combined
  7. What does (?-i) do within a regex?
    A) Enables case sensitivity for part of the pattern
    B) Disables global matching
    C) Ignores multiline settings
    D) Switches off greediness
  8. How can you use conditional regex matching in Perl?
    A) (?(condition)yes-pattern|no-pattern)
    B) [?(condition):yes|no]
    C) (if condition){yes|no}
    D) (condition ? yes : no)
  9. Which modifier is required to execute a regex only once?
    A) /o
    B) /x
    C) /s
    D) /g
  10. What does the (?>...) construct achieve in advanced regex?
    A) Enables atomic grouping
    B) Matches without backtracking
    C) Matches once and skips further checks
    D) All of the above

Answers Table

QNoAnswer (Option with Text)
1B) *
2B) Matches one or more occurrences
3A) {3,5}
4A) Matches zero or one occurrence
5A) ^
6C) \b
7B) Positive lookahead
8B) Matches the end of a string, ignoring a trailing newline
9A) By appending ? after the quantifier
10A) (?!...)
11C) (pattern)
12A) Represents the first capturing group’s value
13D) $2
14A) \1
15A) Non-capturing group
16B) It captures only the last match
17A) Yes, always
18C) Both A and B
19C) $&
20A) Refers to the last capturing group matched
21B) Makes the pattern case-insensitive
22C) /m
23A) Match the pattern globally
24A) Allows use of whitespace and comments for readability
25A) Makes . match newline characters
26A) Concatenate them as /imx
27A) Enables case sensitivity for part of the pattern
28A) `(?(condition)yes-pattern
29A) /o
30D) All of the above

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top