Four usage of regular expression-question mark

Source: Internet
Author: User
Four usage of regular expression-question mark Original Text symbol

Because? The regular expression has a special meaning, so if you want to match? Escape ,\?

Quantifiers or not

A question mark can be used to repeat the preceding content 0 times or once, that is, either not to appear or to appear once.

Non-Greedy match greedy match

Match the string as long as possible. By default, greedy match is used.

string pattern1 = @"a.*c";   // greedy match 
Regex regex = new Regex(pattern1);
regex.Match("abcabc"); // return "abcabc"
Non-Greedy match

When a match is met, use? To indicate non-Greedy match

string pattern1 = @"a.*?c";   // non-greedy match Regex regex = new Regex(pattern1);regex.Match("abcabc"); // return "abc"
Several common non-Greedy match Pattern
  • *? Repeat any time, but as few as possible
  • +? Repeat once or more times, but as few as possible
  • ?? Repeated 0 or 1 times, but as few as possible
  • {N, m }? Repeat n to m times, but as few as possible
  • {N ,}? Repeated more than N times, but as few as possible
No Capture Mode

How do I disable the ability to capture parentheses? Instead, we only use it for grouping by adding :?, Here, the first round arc is used only for grouping, instead of capturing variables. Therefore, the content of $1 can only be steak or burger, but never bronto.
While (<> ){
If (/(? : Bronto) (steak | burger )/){
Print "Fred wants a $1 \ n ";
}
}


Source: http://www.cnblogs.com/graphics/archive/2010/06/02/1749707.html>  

From Weizhi note (wiz)

Four usage of regular expression-question mark

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.