Java Regular expression non-greedy mode __java

Source: Internet
Author: User

non-greedy pattern of regular expressions.

non-greedy patterns match as few matches as possible.

non-greedy mode is represented by adding one after the number of matches. Number of Matches: * + {m,n}

like a regex = "A *?" ".+?"


in non-greedy mode to make the match as few as possible, as short as possible.

The greedy pattern of regular expressions is as much as possible when matching


such as: <div><p>bdxxx</p></div> <div><p>taaaa</p></div>

greedy pattern Regular expression: regex= "<div>.*</div>"

result: Match once. Obtained for <div><p>bdxxx</p></div> <div><p>taaaa</p></div>

non-greedy pattern regular Expressions: Regex = "<div>.*?</div>"

results: Matched two times. Two times acquired respectively for:<div><p>bdxxx</p></div> and <div><p>taaaa</p></div>


non-greedy mode will match as little as possible, and finish the match the first time you encounter </div>. Make next match

The greedy pattern will be matched as much as possible, and continue to match backwards after the first encounter </div> to find the last </div> to complete the match.


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.