Java docs tell us you can have this greedy quantifiers:
X{n} X, exactly n times
X{n,} X, at least n times
X{n,m} X, at least n but not more than m times
There is no mention of having X, at MOST n times
.
So I made a little test:
boolean atMost = Pattern.matches("X{,3}", "XX");
I expected atMost
to be true
, since it's safe to assume the lower bound to be zero.
Instead, I got an exception:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 0
X{,3}
^
Note it's not a thing of being Greedy, Reluctant or Possessive: "X{,3}?"
and "X{,3}+"
also don't work.
I could have used "X{0,3}"
to achieve the pattern I'm looking for, but that's not my question.
Why isn't the X, at MOST n times
regex quantifier included in Java, in the X{,n}
syntax?
How is it like in other programming languages or regex "flavors"?