X, exactly n times
[abc]+
(a or b or c, one
or more times) or (abc)+
(the group "abc", one or more times).
Let's illustrate by specifing the group (dog)
, three times in a row.
Here the first example finds three matches, since the quantifier applies to the entire capturing group. Remove the parenthesis, however, and the match fails because the quantifierCurrent REGEX is: (dog){3} Current INPUT is: dogdogdogdogdogdog Finds the text "dogdogdog" starting at index 0 and ending at index 9. Finds the text "dogdogdog" starting at index 9 and ending at index 18. Current REGEX is: dog{3} Current INPUT is: dogdogdogdogdogdog No match found.
{3}
now applies only to the letter "g".
Similarly, we can apply a quantifier to an entire character class:
Here the quantifierCurrent REGEX is: [abc]{3} Current INPUT is: abccabaaaccbbbc Finds the text "abc" starting at index 0 and ending at index 3. Finds the text "cab" starting at index 3 and ending at index 6. Finds the text "aaa" starting at index 6 and ending at index 9. Finds the text "ccb" starting at index 9 and ending at index 12. Finds the text "bbc" starting at index 12 and ending at index 15. Current REGEX is: abc{3} Current INPUT is: abccabaaaccbbbc No match found.
{3}
applies to the entire character class
in the first example, but only to the letter "c" in the second.