Understanding of Possessive quantity modifiers in Java Regular Expressions

Source: Internet
Author: User

The regular expression specifies the number of delimiters, Such ?, +, *, {N, m} matches the greedy mode by default, for example,. * The result of B matching acbab is that acbab instead of acb Regular Expressions support the lazy mode, that is, a quantity modifier (quantifier) is added after the number limit, which is represented by a question mark, for example:. *? B matches acbab instead of acbab www.2cto.com. Java supports a Possessive quantity modifier in regular expressions, which is expressed by a plus sign. It literally means matching like greedy mode, but not backtracking. What does this mean? In fact, this is related to the matching algorithm. For example, in greedy mode, the regular expression. * When B matches acbac, after the first character a is successfully matched, the matching algorithm will continue to read the characters After character a for matching. * In this case, the greedy mode works, and the cbac behind a can match. * But when the matching algorithm continues to read characters later, it finds that no characters are readable and cannot match B in the regular expression, so it goes back to a character to see if c can match B, no. Continue tracing until it is traced back to B. A. * + B does not trace back when matching. That is to say, when greedy reading the last character and finding that no character matches B, the matching fails. Here are a few examples (AB) * + a can match ababacd. Because the two AB S do not have AB, they cannot greedy? + A matches aaaaa with aa, but a ++ a and a {0, 10} + a fail to match.

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.