SoFunction
Updated on 2025-04-07

Detailed explanation of the four usages of regular expression question marks

Original symbol

Because ? has a special meaning in regular expressions, if you want to match? itself, you need to escape, \?

There are countless words

A question mark can indicate that the previous content is repeated 0 times or once, that is, it either does not appear or appears once.

Non-greedy match

Greedy Match

When matching is satisfied, match as long as possible, and by default, greedy matching is used

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

Non-greedy match

When matching is satisfied, match as short as possible string, use ? to represent non-greedy matches

string pattern1 = @"a.*?c";  // non-greedy match 
Regex regex = new Regex(pattern1);
("abcabc"); // return "abc"

Several commonly used non-greedy matches Patterns

*? Repeat any time, but repeat as few as possible
+? Repeat 1 or more times, but repeat as few as possible
?? Repeat 0 or 1 time, but repeat as little as possible
{n,m}? Repeat n to m times, but repeat as few as possible
{n,}? Repeat more than n times, but repeat as few as possible

Non-capture mode

How to turn off the capture ability of parentheses? But just use it to group, the method is to add:? after the left bracket, here the first parentheses are only used to group, and will not occupy the capture variable, so the content of $1 can only be steak or burger, and it can never be a bronto.

while(<>){
  if(/(?:bronto)(steak|burger)/){
    print "Fred wants a $1\n" ;
  }
}