I'm trying to understand the following regular expression quantifier (a is just an exemplary token here):
a{n}?
How does the question mark affect the match of the above expression? And how does it differ from the following?
a{n}
I would have expected the pattern aa{1}?a
to match both aaa
and aa
for example. While it matches aaa
, aa
is not a match. The pattern a(a{1})?a
does match both, so the parentheses do make a difference here.
Note: The msdn article Quantifiers in Regular Expressions states for both:
The {n} quantifier matches the preceding element exactly n times, where n is any integer.
For {n}?
, it adds the following, not overly helpful part:
It is the lazy counterpart of the greedy quantifier {n}+.
{n}?
quantifier is not part of the POSIX standard, so I added the language I'm working with to avoid ambiguities. – Unawarea(a{1})?a
. But you're right, what's a greedy, exactly counted match? – Unaware