0

can someone give difference between X? ,X?+ and X?? with java example. where X?,X?+ and X?? are java.util.regex.Pattern.

for all three patterns they have given same explanation (X, once or not at all) reference http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

I am not able to find good example in internet

Note: today morning I asked part of question here: what is the difference between the patterns X? and X?, since I have an updated posting it again

Community
  • 1
  • 1
upog
  • 4,965
  • 8
  • 42
  • 81
  • You could work into a more related example like using `aababbabba` and use the patterns `ab?`, `ab?+` and `ab??` – Luiggi Mendoza Sep 12 '13 at 17:01
  • Want to understand the basics of patterns given in java api doc – upog Sep 12 '13 at 17:01
  • 2
    Derp. I made a misassumption. Ignore what I said before (and now nuked.) – millimoose Sep 12 '13 at 17:02
  • Highly advise to go try out the regexes yourself. There are a ton of free online sites where you can test regular expressions to see A) What they do and B) debug them. The one I personally use is http://regexpal.com/ – StormeHawke Sep 12 '13 at 17:04
  • Anyway. The docs say they're *posessive quantifiers*, googling for that gives me this page which seems like a good resource: http://www.regular-expressions.info/possessive.html Basically, they're greedy quantifiers, except the RE engine won't backtrack over them. – millimoose Sep 12 '13 at 17:05
  • I think a good example of reluctant (??) vs normal (?) is the regex `aa??` on the string `aaaa`. You get 4 matches of `a` where you instead get 2 of `aa` with the regex `aa?`. – Bernhard Barker Sep 12 '13 at 17:15
  • There's an explanation of quantifiers [here](http://docs.oracle.com/javase/tutorial/essential/regex/quant.html) – Mauren Sep 12 '13 at 17:19

3 Answers3

3

This is the way I like to think of it -

X??   Negative bias, 0 or 1 time - preference to 0 if possible

X?    Neutral bias,  0 or 1 time

X?+   Positive bias, 0 or 1 time - preference to 1 if possible,
      and if 1 won't give it up (backtrack)
  • +1 for easy answer :) Anyway in `X? ` 1 is preferred, but for grater cause it will also accept 0 and give up matched part. – Pshemo Sep 12 '13 at 17:35
  • I like to think of neutral as a basis for left to right processing. In this model the basis can only be greedy, but this is a confusing word IMO, should not be stressed. –  Sep 12 '13 at 18:05
  • Simple and easy thanks – upog Sep 13 '13 at 04:34
  • I would've called **not** giving up backtracking, but anyway. – Bernhard Barker Sep 13 '13 at 07:45
1

Take a look at these examples

System.out.println("abb".matches("abb?b"));  // Greedy     -> true
System.out.println("abb".matches("abb??b")); // Reluctant  -> true
System.out.println("abb".matches("abb?+b")); // Possessive -> false

Firs two will match because even if ? will be greedy or reluctant ?? second b can be returned and used to match variant of regex where this part wasn't found.

Interesting thing happens in ?+ which is possessive so when it match something then it means that this part belongs to possessive part of regex, and cant be matched by rest of regex. So because second b is matched by b?+ it cant be matched by last b regex, that is why matches returns false.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
1

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).

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194