Quantifiers

Quantifiers control how many times a pattern can match:

  • ? makes a pattern optional (i.e. it matches 0 or 1 times)
  • + lets a pattern repeat (i.e. it matches at least once)
  • * lets a pattern be optional or repeat (i.e. it matches any number of times, including 0).
# ab? matches an "a", optionally followed by a "b".
str_view(c("a", "ab", "abb"), "ab?")
## [1] │ <a>
## [2] │ <ab>
## [3] │ <ab>b
# ab+ matches an "a", followed by at least one "b".
str_view(c("a", "ab", "abb"), "ab+")
## [2] │ <ab>
## [3] │ <abb>
# ab* matches an "a", followed by any number of "b"s.
str_view(c("a", "ab", "abb"), "ab*")
## [1] │ <a>
## [2] │ <ab>
## [3] │ <abb>