You need more complex patterns to see the difference.
A greedy quantifier first matches as much as possible (but backtracks).
A reluctant or "non-greedy" quantifier first matches as little as possible.
A possessive quantifier is just like the greedy quantifier, but it doesn't backtrack.
Use capturing groups to see what is happening.
Try patterns such as (b?)(b+)
, (b??)(b+)
, (b?+)(b+)
on the strings b
and bb
.
Print A) if it matches, and B) if so, what are the groups?
The following is what I would expect, but did not test:
Greedy: it should match on empty and b
in the first case (by backtracking!), b
, b
in the second.
Reluctant: it should match on , `b` in the first case,
, bb
in the second. The first group will actually never match anything, so this pattern does not make sense.
Possessive: it should NOT match the first (no more b
left for second group, and it doesn't backtrack) and b
, b
in the second string (no backtracking needed).
No comments:
Post a Comment