Image

Imageandrogy8 wrote in Imagejava_dev

java.util.regex Pattern

Here's something which is a little odd. In the Sun description of the regex Pattern class, there are three different types of quantifiers: Greedy quantifiers, Reluctant quantifiers, and Possessive quantifiers. The puzzling part is that they have different names but seem to behave exactly the same. Here's what I mean - I've just included the first few examples from each category to make the point, which is that the notation is different but the functionality looks (to me) very much the same:

Greedy quantifiers:
X?      X, once or not at all
X*     X, zero or more times
X+     X, one or more times

Reluctant quantifiers:
X??   X, once or not at all
X*?   X, zero or more times
X+?   X, one or more times

Possessive quantifiers:
X?+    X, once or not at all
X*+    X, zero or more times
X++    X, one or more times

So I'm just curious, why did the Java language designers include three different ways to do several different tasks? I was wondering if certain languages used one way, and certain other languages used another way, and perhaps the Java language designers combined them all into one. Or perhaps any language that has regular expressions always supports these three ways, and Java is just following suit.

Given that there are three different ways to say zero or more times, is it more common in practice, I wonder, to write X*, X*?, or X*+?. Why would a coder use one over the other, given that they are all logically equivalent?