X[A-Z]+
The new expression matches the same set of strings as the previous one, but does not capture the prefix of X characters.
Regular Expression | |||
String | |||
Group 0 | |||
Group 1 | Group 2 | Group 3 | Group 4 |
An quantifier in a regular expression may be greedy (the default), reluctant, or possesive. A reluctant quantifier does this:
An quantifier is made reluctant by appending a question mark.
The expression X+?
matches X one or more times,
but matches as few X as it can without preventing other quantifier from matching.
For example, when the expression
(X+?)([A-Z]+)
matches the string
XXXX
the reluctant X+?
could potentially match
the entire string.
But it
matches as little as possible: the first X .
The final [A-Z]+
of the expression matches the rest the string.
Reluctant quantifier are sometimes called lazy quantifier, because they match as little as possible.
Here is a string: XXXXX
What parts of the string match the capture group in the following expressions?
(X+)[A-Z]+
(X+?)[A-Z]+