Mega Code Archive

 
Categories / C# Book / 11 Regular Expression Basics
 

0646 Greedy Versus Lazy Quantifiers

By default, quantifiers are greedy, not lazy. A greedy quantifier repeats as many times as it can before proceeding. A lazy quantifier repeats as few times as it can before proceeding. You can make any quantifier lazy by suffixing it with the ? symbol. To illustrate the difference, consider the following HTML fragment: string html = "<i>This</i> is a<i>test</i> case"; Suppose we want to extract the two phrases in italics. If we execute the following: using System; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { string html = "<i>This</i> is a<i>test</i> case"; foreach (Match m in Regex.Matches (html, @"<i>.*</i>")) Console.WriteLine (m); } } The output: This is atest The result is not two matches, but a single match, as follows: string html = "<i>This</i> is a<i>test</i> case"; * quantifier greedily repeats as many times as it can before matching </i>. Make the quantifier lazy: using System; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { string html = "<i>This</i> is a<i>test</i> case"; foreach (Match m in Regex.Matches (html, @"<i>.*?</i>")) Console.WriteLine (m); } } The output: Thistest