Regex Named Capture Groups Essentials Quiz Quiz

Explore the practical uses and syntax of named capture groups in regular expressions. This quiz covers how to define, reference, and work with named capture groups for better readability and maintainability in pattern matching.

  1. Defining Named Capture Groups Syntax

    Which of the following is the correct syntax for defining a named capture group called 'year' in a regular expression?

    1. (?<year>d{4})
    2. (#yeard{4})
    3. ({year}d{4})
    4. [year]d{4}

    Explanation: The correct syntax is '(?<year>d{4})', where the group name is enclosed within angle brackets after a question mark. '({year}d{4})' and '[year]d{4}' are incorrect as they use curly braces or square brackets, which are not valid for named groups. '(#yeard{4})' uses a hash instead of the required angle brackets, making it invalid as well.

  2. Referencing Named Groups in Replacement Patterns

    When performing a replacement operation, how do you reference a named capture group called 'word' in the replacement string?

    1. ${word}
    2. $<word>
    3. g<word>
    4. %{word}

    Explanation: The correct way to reference a named capture group in the replacement string is '${word}', which will be replaced by the text matched by the 'word' group. '$<word>' and 'g<word>' are not valid in replacement strings, though 'g<word>' is sometimes used in matches. '%{word}' is an incorrect placeholder and not used in regex replacement contexts.

  3. Multiple Named Groups in a Single Regex

    Consider the pattern '(?<first>w+) (?<last>w+)' applied to the string 'Alex Brown'. What values will the named groups 'first' and 'last' capture?

    1. 'Alex' for 'first' and 'Brown' for 'last'
    2. 'Alex Brown' for 'first' and undefined for 'last'
    3. 'A' for 'first' and 'B' for 'last'
    4. Both groups will capture the whole string 'Alex Brown'

    Explanation: The named group 'first' captures 'Alex', and 'last' captures 'Brown', as the pattern matches two consecutive word groups separated by a space. 'Alex Brown' for one group and undefined for the other is incorrect because both groups have definitions. Single letters such as 'A' and 'B' would only be matched with patterns like 'w'. Capturing the whole string in both groups does not reflect correct group boundaries.

  4. Advantages of Named Capture Groups

    What is a primary benefit of using named capture groups instead of numeric (unnamed) ones in lengthy regular expressions?

    1. They improve readability and make referencing captures easier by name.
    2. Named groups make the regular expression run faster.
    3. Only named groups can be nested inside other groups.
    4. Named groups can match longer text than numeric groups.

    Explanation: Named capture groups enhance readability by allowing references by descriptive names rather than numbers, especially in complex patterns. They do not improve performance ('run faster') compared to numeric groups. Both named and numeric groups can be nested, so there is no exclusivity. The length of text matched depends on the pattern, not on whether the group is named or not.

  5. Handling Optional Named Groups

    Given the pattern '(?<area>d{3})?(?<local>d{7})' and text '1234567890', what happens if the 'area' named group is made optional using the question mark?

    1. 'area' captures '123' and 'local' captures '4567890'
    2. 'area' captures '1234567' and 'local' is empty
    3. 'area' is always empty because it's optional
    4. Both 'area' and 'local' capture the entire string

    Explanation: The optional quantifier allows the 'area' group to match the first three digits if present; the 'local' group then matches the next seven digits. Saying 'area' always captures '1234567' or is always empty ignores the order and limits of the pattern. Both groups capturing the entire string is incorrect; each group captures only what its subpattern allows.