tags:

views:

827

answers:

3

From the Java 6 Pattern documentation:

Special constructs (non-capturing)

(?:X)   X, as a non-capturing group

(?>X)   X, as an independent, non-capturing group

Between (?:X) and (?>X) what is the difference? What does the independent mean in this context?

+12  A: 

It means that the grouping is atomic, and it throws away backtracking information for a matched group. So, this expression is possessive; it won't back off even if doing so is the only way for the regex as a whole to succeed. It's "independent" in the sense that it doesn't cooperate, via backtracking, with other elements of the regex to ensure a match.

erickson
A: 

It means that the grouping is atomic, throwing away backtracking information that would support non-possessive capturing.

So what does that mean then?

Orion Edwards
+1  A: 

if you have (foo(?>co)*co), that will never match. im sure there are practical examples of when this would be useful, try the o'reilly book.