Quantifiers
The following regular expression characters add optional quantity data to a regular expression. A quantifier expression applies to the character, group, or character class that immediately precedes it.
Regular Expression | Description |
---|---|
* | Zero or more matches. For example, \d* matches zero or more consecutive digits. Equivalent to {0,}. |
+ | One or more matches. For example, \d+matches one or more consecutive digits (i.e., a positive whole number). Equivalent to {1,}. |
? | Zero or one matches. For example, \d? matches a single digit or a blank value. Equivalent to {0,1}. |
{n} | Exactly n matches. For example, (pizza){2} only matches "pizzapizza". |
{n,} | At least n matches. For example, (abc){2,} matches "abcabc" and "abcabcabc", but not "abc". |
{n,m} | At least n, but no more than m, matches. For example, \d{2,4} matches a two digit, three digit, or four digit number. |
Note: By default, .NET regular expressions are "greedy." You can add a question mark "?" after these quantifiers to make them "lazy." For example, Name:\s(.+)\s matches "John Last Name:" in "First Name: John Last Name: Smith," and Name:\s(.+?)\s matches "John".