Using Regular Expressions Metacharacters with .net – Greedy and Lazy quantifier
When a regular expression has a quantifier that can accept a range of repetitions (like ".*"), the normal behavior is to match as many characters as possible. Consider the following regular expression:
a.*b The longest string starting with a and ending with b
If this is used to search the string "aabab", it will match the entire string "aabab". This is called "greedy" matching. Sometimes, we prefer "lazy" matching in which a match using the minimum number of repetitions is found. All the quantifiers in Table 2 can be turned into "lazy" quantifiers by adding a question mark "?". Thus "*?" means "match any number of repetitions, but use the smallest number of repetitions that still leads to a successful match". Now let’s try the lazy version of example:
a.*?b The shortest string starting with a and ending with b
If we apply this to the same string "aabab" it will first match "aab" and then "ab".




