Understanding of possessive number modifiers in Java regular expressions

Source: Internet
Author: User
Tags modifiers
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.