Regular Expressions Quiz - Languages Perl
Free practice quiz: Regular Expressions (Languages Perl). 43 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
This Languages Perl practice quiz has 43 multiple-choice questions on Regular Expressions, and 43 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.
Start quiz: 43 questions, freeSample questions
In Perl regular expressions, what does the `+` quantifier match?
Show answer
Answer: B. One or more occurrences of the preceding element.
The `+` quantifier in Perl (and many other regex engines) is a greedy quantifier that matches the preceding element one or more times.
What is the purpose of the `?` quantifier in Perl regular expressions?
Show answer
Answer: A. Matches zero or one occurrence of the preceding element.
The `?` quantifier matches the preceding element zero or one time. It is the lazy version of the `+` quantifier.
In Perl's `split` function with a regular expression, what happens if the regex matches an empty string?
Show answer
Answer: B. An empty string is added to the list of results at that position.
If a regex used with `split` matches an empty string, Perl inserts an empty string into the resulting list for each such match. This can lead to unexpected behavior if not handled carefully.
What does `\G` anchor match in Perl regular expressions?
Show answer
Answer: C. The position where the previous `m//` match ended (or the start of the string on the first match).
The `\G` anchor is used for incremental matching. It matches at the position where the previous match of the same regular expression ended. On the first match attempt, it behaves like `^`.
What does `(.*?)` match in the string 'abcdeabcde'?
Show answer
Answer: C. The shortest possible match for 'abcde'.
The `.*?` is a lazy match for any character (`.`) zero or more times (`*?`). When used in `(.*?)`, it will match the shortest possible substring that allows the rest of the regex to succeed. If the regex after it requires something specific, it will match up to that point. In this case, if the regex were `(.*?)abcde`, it would match 'abcde' for the first group and then `(.*?)` would match the empty string before the second 'abcde'. If the `(.*?)` is the end of the regex, it will match the shortest string, typically an empty string.
More questions in this quiz
Answers and explanations are in the full quiz.
- What is the special meaning of `.` in Perl regular expressions?
- Which Perl regular expression operator is used for substitution?
- In a Perl regex, what does `\d` represent?
- What is the difference between `*` and `*?` in Perl regular expressions?
- Which character is used to escape a special character in a Perl regular expression?
- What does the `^` anchor match in a Perl regular expression?
- In Perl's `s///` operator, what does the `g` modifier do?
- Which character class matches any word character (alphanumeric plus underscore) in Perl regex?
- What does `[^...]` represent in a Perl regular expression character set?
- In Perl, the `m//` operator can be used with the `i` modifier. What does `i` do?
- What is the role of the `|` operator in Perl regular expressions?
- What does the `\s` shorthand character class match in Perl regex?
- In Perl's `s///` operator, what is the default behavior if the `g` modifier is NOT used?
- What is the purpose of `(?:...)` in Perl regular expressions?
- What does the `` assertion match in Perl regex?
- What does `$` match at the end of a Perl regular expression?
- How do you match an escaped literal parenthesis `(` in a Perl regular expression?
- What does `[a-z]` match in a Perl character set?
- What is the purpose of the `s///r` modifier in Perl's substitution operator?
- Which Perl regex construct matches a position that is not a word boundary?
- What does `\{\}` match literally in a Perl regex?
- What is the result of `m/a*b/` on the string 'bbb' in Perl?
- In Perl's `split`, what happens if the delimiter is `//` (an empty regex)?
- What is the purpose of `(?=...)` in Perl regex?
- What does `.` match with the `s` modifier (like `m/.../s`) in Perl regex?
- What is the meaning of `^` inside a character class `[...]` in Perl regex?
- Which Perl regex construct matches a position that is NOT followed by the pattern inside the parentheses?
- What does `\w` match in Perl regex?
- In Perl's `s///` operator, what does the `i` modifier do?
- What is the purpose of `(?<=...)` in Perl regular expressions?
- What is the behavior of `\d` when the `x` modifier is used in Perl regex?
- What does `\h` match in Perl regular expressions?
- What is the purpose of `(?<!...)` in Perl regular expressions?
- What does the `P` modifier (as in `s/a/b/P`) do in Perl substitution?
- What does `\w{5}` match?
- What is the difference between `()` and `(?:)` in Perl regex?
- What does the `(?<name>...)` syntax denote in Perl regex?
- What is the primary use of the `tr///` or `y///` operator in Perl?