If you don't know what you're looking for
So far you've used str_detect()
which returns TRUE
if the pattern matches and FALSE
otherwise. But regular expressions are also excellent at extracting the searched term from a larger amount of text. You can use the str_match()
function for that.
The next special character you'll get to know is the period: "."
. The period matches any character, it's like a wild card. So if you search for example for "..."
you will find three characters - be it letters or numbers or even white spaces.
This is pretty handy, except if you need to search for an actual full stop "."
. In that case: escape the period with two backslashes: "\\."
This exercise is part of the course
Intermediate Regular Expressions in R
Exercise instructions
- Match not only
Saw 4
but also the other sequels. - Match the first four characters of all the movie titles that start with
"K"
. - Detect the movie that ends with an actual full stop
"."
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Here's an example pattern that will find the movie Saw 4
str_match(movie_titles, pattern = "Saw 4")
# Match all sequels of the movie "Saw"
str_match(movie_titles, pattern = "___")
# Match the letter K and three arbitrary characters
str_match(movie_titles, pattern = "^K___")
# Detect whether the movie titles end with a full stop
str_detect(movie_titles, pattern = "___$")