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.
Diese Übung ist Teil des Kurses
Intermediate Regular Expressions in R
Anleitung zur Übung
- 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"
.
Interaktive Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
# 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 = "___"
)]