Using backreferences in patterns
Backreferences can be useful in matching because they allow you to find repeated patterns or words. Using a backreference requires two things: you need to capture()
the part of the pattern you want to reference, and then you refer to it with REF1
.
Take a look at this pattern: capture(LOWER) %R% REF1
. It matches and captures any lower case character, then is followed by the captured character: it detects repeated characters regardless of what character is repeated. To see it in action try this:
str_view(c("hello", "sweet", "kitten"),
pattern = capture(LOWER) %R% REF1)
If you capture more than one thing you can refer to them with REF2
, REF3
etc. up to REF9
, counting the captures from the left of the pattern.
Let's practice with boy_names
again. You might notice a change in this dataset. We've converted all names to lower case; you'll learn how to do that in the next chapter.
This exercise is part of the course
String Manipulation with stringr in R
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Names with three repeated letters
repeated_three_times <- ___
# Test it
str_view(boy_names, pattern = repeated_three_times, match = TRUE)