\d{n,} - added comma after number means that preceding character (a digit in this example) should repeat n times or more (up to countless). So, regular expression \w{5,} will look for five or more white spaces in sequence.

\d{n,m} - preceding character should repeat at least n times and no more than m times. For example, \d{3,6} is a number that contains from 3 to 6 digits. Expression [a-z]{3,5} will search for a 3 to 5 lower case letters in sequence.

2. <b>Hi!</b><br />I <b>love</b> regular expressions - also comply, both marked strings start with <b> and end with </b>

First solution is named as "greedy", because matches largest possible match. Second solution is known as "nongreedy" because it matches smallest possible match.

All quantifiers listed in previous section are known as greedy quantifiers and produce output like first result in previous example, trying to match largest possible string that comply to regular expression contidions. Nongreedy (or lazy) quantifiers are quantifiers that returns results with smallest possible match. Lazy quantifiers are marked with additional ? character. We'll compare greedy and lazy quantifiers in next table:

Greedy quantifier
(matches largest possible string)

Lazy quantifer
(matches smallest possible string)

Quantifier description

*

*?

zero or more occurencies

+

+?

one or more occurencies

?

??

zero or one

{n}

{n}?

exactly n times

{n, }

{n,}?

at least n times

{n,m}

{n,m}?

between n and m occurencies

Conclusion

Notice that there is no simple answer on question which quantifier is faster: greedy or non-greedy (often called lazy)? Sometimes you must use non-greedy quantifier to get correct results, but often both greedy and lazy quantifiers could be used for the same problem. In that case, execution speed depends of specific case. In general, non-greedy quantifier looks forward, takes character by character and compares it; greedy quantifier workds backward, it removes characters until finds a match.

There is online application you can use to Test .Net Regular Expressions on your text. In next tutorial, I will explain .Net Regular Expressions Groups. Happy coding!