You can specify a specific number of repetitions with the {m}
syntax, where m is the number of repetitions expected:
A{4}B{4}
would require exactly four A
and four B
characters.
There is similar syntax to specify a range instead of a fixed number; from the Regular Expression syntax documentation:
{m}
Specifies that exactly m copies of the previous RE should be matched; fewer matches cause the entire RE not to match. For example, a{6}
will match exactly six 'a'
characters, but not five.
{m,n}
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as many repetitions as possible. For example, a{3,5}
will match from 3 to 5 'a'
characters. Omitting m specifies a lower bound of zero, and omitting n specifies an infinite upper bound. As an example, a{4,}b
will match aaaab
or a thousand 'a'
characters followed by a b
, but not aaab
. The comma may not be omitted or the modifier would be confused with the previously described form.
{m,n}?
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as few repetitions as possible. This is the non-greedy version of the previous qualifier. For example, on the 6-character string 'aaaaaa'
, a{3,5}
will match 5 'a'
characters, while a{3,5}?
will only match 3 characters.