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