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.
Which regex syntax correctly creates a capturing group to match the substring 'cat' in the text?
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.
Given the pattern (?:dog|cat), what purpose does the '?:' serve inside the parentheses?
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: '(?!...)'.
If the regex pattern (d{2})-(d{2}) is applied to '12-34', what value does the second capturing group contain?
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.
Which expression correctly defines a named capturing group that matches a three-letter word?
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.
Which option correctly uses a backreference to match a repeated word captured earlier as '(w+)'?
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.