Starts with, ends with
You've already seen how you can search for certain characters at the beginning of a string using the caret "^"
. Of course, regular expressions also offer a way to search for things at the end of a string. This is what the dollar sign "$"
will do.
When creating a pattern to look for something at the beginning of a line, use the caret followed by a search term "^___"
. When looking for something at the end, type the search term first and then append the dollar sign "___$"
. The order of appearance is very important when creating regular expressions.
This exercise is part of the course
Intermediate Regular Expressions in R
Exercise instructions
- Run the first line to have a look at the movie titles you're working with.
- Create a pattern that lists all the movies that start with
"The"
. - Create a pattern that searches movies that end with
"3D"
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Familiarize yourself with the vector by printing it
movie_titles
# List all movies that start with "The"
movie_titles[str_detect(
movie_titles,
pattern = "___"
)]
# List all movies that end with "3D"
movie_titles[str_detect(
movie_titles,
pattern = "___"
)]