`XXXXXrats`

`(X{2,5})\w*`

- the group matches
`XXXX`

- the group matches
`(X{2,5}?)\w*`

- the group matches
`XX`

- the group matches
`(X*?)\w*`

- the group matches the empty string, and the rest of the RE matches the complete string

An quantifier in a regular expression may be
*greedy* (the default), *reluctant*, or *possesive*.
A **possesive** quantifier does this:

- The match starts with the first unmatched character in the string.
- The possessive quantifier starts matching from left to right one character at a time.
- The possessive quantifier matches as many characters as it can.
- Once a character has been matched, it remains in possession of the quantifier, even if this prevents other parts of the expression from matching.

An quantifier is made possessive by appending a plus sign.
The expression `X++`

matches X one or more times,
and never gives up what it has matched.
For example, the expression

`X++[A-Z]+`

__does match__ the string

XXXXZ

The `X++`

part of the expression eats up all the X's leaving the Z
to be matched by the rest of the expression.

However, the expression __does not match__ the string

XXXX

because the `X++`

eats the entire expression, and does not give up
the last X for the code `[A-Z]+`

to match.

Java regular expressions support possessive quantifiers, but JavaScript does not, so there interactive applet on this page.

Is the entire string

YYXXratsXX

matched by the regular expression

`([XY]++)rats\1`

(Note: If you try this in the applet, remember that the applet finds substrings that match the regular expression. When the entire string matches the expression, "Group 0" and "String" will be identical.)