Sometimes, you don't know how many characters there are to match. In order to accommodate that kind of uncertainty, Regular Expressions support the concept of quantifiers. These quantifiers let you specify how many times a given component of your Regular Expression must occur for your match to be true.
The following table illustrates the various quantifiers and their meanings:
Character | Description |
* | Matches the preceding sub-expression zero or more times. |
+ | Matches the preceding sub-expression one or more times. |
? | Matches the preceding sub-expression zero or one time. |
{n} | Matches exactly n times, where n is a non-negative integer.. |
{n,} | Matches at least n times, where n is a non-negative integer |
{n,m} | Matches at least n and at most m times. m and n are non-negative integers, where n <= m. |
n and m are integers.
With a large input document, chapter numbers could easily exceed nine, so you need a way to handle chapter numbers with more than one digit. Quantifiers give you that capability. The following JScript .NET Regular Expression matches chapter headings with any number of digits:
/Chapter [1-9][0-9]*/
The first bracket expression [1-9] makes sure that the first digit is in the range 1-9. The second bracket expression with quantifier [0-9]* searches for 0 or more digits in the range 0-9. The quantifier (*) appears after the range expression.