Java Regular Expression quantifier---three matching patterns "greedy, reluctant, possessive"

Source: Internet
Author: User

1, Greediness (Greedy type):Maximum Match
X, x*, x+, X{n,} are allmaximum match. For example you want to use "<.+>" to Match "A<tr>aava</tr>abb", perhaps the result you expect is to match "<tr>", but the actual result will be matched to "<tr>aava</ Tr> ". What is this for? Below we track the matching process for the maximum match.
① "<" matches the string "<". ② ". +" matches the string "Tr>aava</tr>ab", when the maximum match, it matches two ">", it matches all characters until the last character of the text "B" ③ at this time, found that cannot successfully match ">", began to follow the original path fallback, with "A" matches ">" until ">" in front of "AB" is successfully matched.
2, reluctant (laziness)(barely type): minimum Match
X, x*, x+, X{n,} are the maximum matches. All right, add one? It becomes a laziness match. For example X??、 x*, x+, X{n,}? are the minimum matches, in fact x{n,m}? and X{n}? Some superfluous.
Minimum matching means,. +? After matching a character, try the > match immediately and fail, then. +? Match one character, then try > match at once. JDK document greedy and reluctant, it is eat a mouthful to metaphor, so translated into greedy and (reluctantly) anorexia most appropriate. But I like the idea of the biggest match and the smallest match.
3, possessive (possessive type): exact match

Unlike the maximum match, there is also a matching form: x?+, x*+, x + +, x{n,}+, and so on, becoming an exact match. It matches all characters until the end of the text, but it is not returned by the original path, as with the maximum match. In other words, a match, can not make it even, to also simply, I like.

Java Regular Expression quantifier---three matching patterns "greedy, reluctant, possessive"

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.