Thursday, April 25, 2019

Example for java regex X?? ,X?+ and X?

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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...