Greedy match and non-greedy match

Source: Internet
Author: User



Greedy match: Matches the string as long as possible when matching matches, by default, greedy match

String pattern1 = @ "A.*c"; Greedy match regex regex = new Regex (PATTERN1); regex. Match ("Abcabc"); Return "ABCABC"

non-greedy match : matches the shortest possible string, using the? To represent a non-greedy match when the match is met

String pattern1 = @ "A.*?c"; Non-greedy match Regex regex = new Regex (PATTERN1); regex. Match ("Abcabc"); Return "ABC"

Several commonly used non-greedy matching pattern

*? Repeat any number of times, but with as few repetitions as possible +? Repeat 1 or more times, but repeat as little as possible?? Repeat 0 or 1 times, but repeat {n,m} as little as possible. Repeat N to M times, but repeat {n,} as little as possible. Repeat more than n times, but repeat as little as possible

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/77/DB/wKioL1ZvzI3xJ9TTAAJ4eW9k7oo890.png "title=" Regular expressions. png "alt=" Wkiol1zvzi3xj9ttaaj4ew9k7oo890.png "/>

This article is from the "bit accumulation" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1723247

Greedy match and non-greedy 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.