Regex Grouping and Capturing Patterns Quiz Quiz

Explore the essential concepts of grouping and capturing in regular expressions. This quiz assesses your understanding of how grouping constructs, numbered and named captures, and differences between capturing and non-capturing groups work in regex patterns.

  1. Identifying Basic Grouping Syntax

    Which regex syntax correctly creates a capturing group to match the substring 'cat' in the text?

    1. (cat)
    2. [cat]
    3. {cat}
    4. <cat>

    Explanation: The correct answer is '(cat)' because parentheses define a capturing group in regular expressions, allowing 'cat' to be captured separately. '[cat]' represents a character class matching any single character 'c', 'a', or 't'. '{cat}' is not valid grouping syntax and would produce a syntax error in most engines. '<cat>' has no special meaning in standard regex grouping.

  2. Purpose of Non-Capturing Groups

    Given the pattern (?:dog|cat), what purpose does the '?:' serve inside the parentheses?

    1. It prevents the group from capturing the match.
    2. It makes the group optional.
    3. It matches at most once.
    4. It inverts the match logic of the group.

    Explanation: Non-capturing groups use '?:' to allow the group to group pattern elements without capturing them for later reference. Making the group optional would require a '?' after the group, like '(?:dog|cat)?'. Matching at most once is controlled by quantifiers, not by '?:'. Inverting match logic, like a negative lookahead, uses a different syntax: '(?!...)'.

  3. Retrieving Captured Groups

    If the regex pattern (d{2})-(d{2}) is applied to '12-34', what value does the second capturing group contain?

    1. 34
    2. 12
    3. 12-34
    4. 1234

    Explanation: In this pattern, '(d{2})' captures two digits before the dash, and the second '(d{2})' captures the two digits after the dash. So, the second group contains '34', while '12' is in the first group. '12-34' is the entire match, not a specific group, and '1234' does not match the pattern structure.

  4. Identifying Named Capture Groups

    Which expression correctly defines a named capturing group that matches a three-letter word?

    1. (?<word>w{3})
    2. ([word]w{3})
    3. (name: w{3})
    4. (#word#w{3})

    Explanation: The correct syntax for a named group is '(?<word>w{3})', where 'word' is the name and 'w{3}' matches exactly three word characters. '[word]' defines a character class, not a group. '(name: w{3})' and '(#word#w{3})' are not valid named capturing syntaxes in standard regex.

  5. Backreferencing with Group Numbers

    Which option correctly uses a backreference to match a repeated word captured earlier as '(w+)'?

    1. (w+) 1
    2. (w+) $1
    3. (w+) $1
    4. (w+) #:1

    Explanation: The sequence '(w+)' captures one or more word characters, and '1' refers to the value of the first captured group. '$1' may be used in replacement sections but not usually in the matching pattern. '$1' is not standard and would just match a literal dollar sign with a one. '#:1' is not a recognized regex syntax for referencing groups.