Basic Quantifiers * + ?
Quantifiers specify how many times the preceding element should repeat.
The Three Basic Quantifiers
| Quantifier | Meaning |
|---|---|
* | Zero or more |
+ | One or more |
? | Zero or one (optional) |
Asterisk (*) - Zero or More
a* matches zero or more 'a' characters.
Notice it matches even with zero 'o's (ggle)!
Plus (+) - One or More
a+ requires at least one 'a'.
Now ggle doesn't match - we need at least one 'o'.
Question Mark (?) - Optional
a? matches zero or one 'a' - making it optional.
The 'u' is optional, so both spellings match.
Combining Quantifiers
Quantifiers with Character Sets
Practice Playground
Try:
\d+- match numbers[a-z]+- match words\w+- match word charactersou?- optional 'u'
Key Takeaways
*matches zero or more (can match nothing)+matches one or more (requires at least one)?makes the previous element optional- Quantifiers apply to the element immediately before them
- Use with character sets:
[a-z]+matches words

