Is the entire string
YYXXratsXX
matched by the regular expression
([XY]++)rats\1
No. ([XY]++)
matches YYXX
and keeps possession that substring.
So group 1 is YYXX and so \1
does not match the XX at the end of the string.
Any of the quantifiers can be made possessive
Possessive Quantifiers matches as many of X as it can and keeps hold of them | |
---|---|
Quantifier | Description |
X?+ | zero or one X |
X*+ | zero or more X |
X++ | one or more X |
X{n}+ | exactly n X |
X{n,}+ | n or more X |
X{n,m}+ | n up to m X |
For example, X{2,5}+
will match between two and five X,
and keeps what it has matched.
The expression X{2}+
must match exactly two X
and is equivalent to X{2}?
and is equivalent to X{2}
.
Which of the following regular expressions match the complete string abc123defg ?
.*[0-9]+.*
.*+[0-9]+.*
.*[0-9]++.*
.*[0-9]+.*+