Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Intermediate Regular Expressions in R

Cursus bekijken

Oefeninstructies

  • 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".

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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 = "___"
)]
Code bewerken en uitvoeren