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)
Which quantifier matches zero or more occurrences of a pattern? A) + B) * C) ? D) {n,}
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
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)
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
Which assertion matches the start of a string? A) ^ B) $ C) \A D) \z
How do you match a word boundary in Perl regex? A) \B B) \W C) \b D) \D
What does the (?=...) assertion represent? A) Negative lookahead B) Positive lookahead C) Positive lookbehind D) Negative lookbehind
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
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 ?
Which assertion checks if a pattern does not follow a specific text? A) (?!...) B) (?<=...) C) (?>...) D) (?<!...)
Capturing Groups and Backreferences (10 Questions)
How is a capturing group defined in Perl? A) [pattern] B) {pattern} C) (pattern) D) <pattern>
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
Which syntax refers to the second capturing group in Perl? A) \1 B) \2 C) $1 D) $2
How do you match the exact same text as the first capturing group? A) \1 B) \2 C) (?:group) D) $1
What does (?:...) represent in Perl regex? A) Non-capturing group B) Positive lookbehind C) Negative lookahead D) Capturing group
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
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
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
Which backreference corresponds to the entire match in Perl? A) $0 B) $1 C) $& D) $+
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)
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
Which modifier enables multi-line matching? A) /s B) /i C) /m D) /g
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
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
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
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
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
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)
Which modifier is required to execute a regex only once? A) /o B) /x C) /s D) /g
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
QNo
Answer (Option with Text)
1
B) *
2
B) Matches one or more occurrences
3
A) {3,5}
4
A) Matches zero or one occurrence
5
A) ^
6
C) \b
7
B) Positive lookahead
8
B) Matches the end of a string, ignoring a trailing newline
9
A) By appending ? after the quantifier
10
A) (?!...)
11
C) (pattern)
12
A) Represents the first capturing group’s value
13
D) $2
14
A) \1
15
A) Non-capturing group
16
B) It captures only the last match
17
A) Yes, always
18
C) Both A and B
19
C) $&
20
A) Refers to the last capturing group matched
21
B) Makes the pattern case-insensitive
22
C) /m
23
A) Match the pattern globally
24
A) Allows use of whitespace and comments for readability
25
A) Makes . match newline characters
26
A) Concatenate them as /imx
27
A) Enables case sensitivity for part of the pattern