Grouping and capturing

  • Parentheses can be used to create capturing groups.
  • Refer back to a match
    • \1 refers back to match of the first parenthesis
    • \2 refers back to match of the second parenthesis, etc
#the following pattern finds all fruits that have a repeated pair of letters
str_view(fruit, "(..)\\1")
##  [4] │ b<anan>a
## [20] │ <coco>nut
## [22] │ <cucu>mber
## [41] │ <juju>be
## [56] │ <papa>ya
## [73] │ s<alal> berry
#this one finds all words that start and end with the same pair of letters:
str_view(words, "^(..).*\\1$")
## [152] │ <church>
## [217] │ <decide>
## [617] │ <photograph>
## [699] │ <require>
## [739] │ <sense>