An understanding of possessive number modifiers in Java regular expressions the regular expression for the number qualifier such as, +, *, {n, m} matches by default is greedy mode, for example: a.*b match acbab result is acbab instead of ACB regular expression support lazy mode, That is, add a number modifier (quantifier) to the number qualifier, with a question mark, such as: A.*?b matches the acbab result is ACB instead of Acbab and the Java language supports a possessive number modifier in regular expressions, denoted by a plus sign. It literally means the same as greedy pattern, but does not backtrack. What the hell does that mean? In fact, this is related to the matching algorithm, such as: In greedy mode, the regular expression a.*b in the match Acbac, after matching the first character a successfully, the matching algorithm will continue to read the characters after the character a match. *, then the greedy mode is working, a back of the CBAC can match. But when the matching algorithm continues to read the characters, it is found that no characters can be read, unable to match the regular expression in the B, so the back of a character, see if C can match B, can not, continue to backtrack, until the trace to B, only match to the ACB. And A.*+b in the match is not backtracking, that is, when the greedy to read the last character found that no characters and B match, it is believed that the match failed. A few more examples (AB) *+a match ABABACD can match to Ababa, because there is no AB after two AB, can not be greedy a?+a match aaaaa will match to AA, but A++a and a{0, 10}+a will match the failure
Understanding of possessive number modifiers in Java regular expressions