Any2Quiz
Computer Science / Languages / Perl

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.

Play all 43 questions free

Sample questions

  1. In Perl regular expressions, what does the `+` quantifier match?

    1. Zero or one occurrence of the preceding element.
    2. One or more occurrences of the preceding element.
    3. Zero or more occurrences of the preceding element.
    4. Exactly one occurrence of the preceding element.
    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.

  2. What is the purpose of the `?` quantifier in Perl regular expressions?

    1. Matches zero or one occurrence of the preceding element.
    2. Matches one or more occurrences of the preceding element.
    3. Matches zero or more occurrences of the preceding element.
    4. Matches exactly one occurrence of the preceding element.
    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.

  3. In Perl's `split` function with a regular expression, what happens if the regex matches an empty string?

    1. An error occurs.
    2. An empty string is added to the list of results at that position.
    3. The match is skipped.
    4. The function returns `undef`.
    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.

  4. What does `\G` anchor match in Perl regular expressions?

    1. The start of the string.
    2. The end of the string.
    3. The position where the previous `m//` match ended (or the start of the string on the first match).
    4. A word boundary.
    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 `^`.

  5. What does `(.*?)` match in the string 'abcdeabcde'?

    1. The entire string 'abcdeabcde'.
    2. The first 'abcde'.
    3. The shortest possible match for 'abcde'.
    4. No match.
    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.

Take the full quiz

More quizzes

Browse all Perl quizzes