XXXXXrats
(X{2,5})\w*
(X{2,5}?)\w*
(X*?)\w*
An quantifier in a regular expression may be greedy (the default), reluctant, or possesive. A possesive quantifier does this:
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.)