210-255 · Question #8
Which string matches the regular expression r(ege)+x?
The correct answer is B. regeegex. The pattern r(ege)+x requires the group 'ege' to repeat one or more times between 'r' and 'x', and 'regeegex' satisfies this with two consecutive 'ege' repetitions.
Question
Which string matches the regular expression r(ege)+x?
Options
- Arx
- Bregeegex
- Cr(ege)x
- Drege+x
How the community answered
(24 responses)- A4% (1)
- B79% (19)
- C13% (3)
- D4% (1)
Why each option
The pattern r(ege)+x requires the group 'ege' to repeat one or more times between 'r' and 'x', and 'regeegex' satisfies this with two consecutive 'ege' repetitions.
rx contains no 'ege' group at all, but the + quantifier requires at least one occurrence of the group, so rx cannot match.
The regex r(ege)+x matches 'r', then one or more occurrences of the capturing group 'ege', then 'x'. The string 'regeegex' decomposes as r + ege + ege + x - the substring 'egeege' is exactly two back-to-back occurrences of 'ege', satisfying the (ege)+ requirement and producing a valid full match.
r(ege)x contains literal parenthesis characters, which are not present in the regex pattern where parentheses serve as grouping metacharacters rather than literals.
rege+x is a different regex where + applies only to the single character 'e', not to the group 'ege', so it is a distinct pattern and not a valid match for r(ege)+x.
Concept tested: Regex capturing group with one-or-more quantifier
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Groups_and_backreferences
Topics
Community Discussion
No community discussion yet for this question.