ComeçarComece de graça

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.

Este exercício faz parte do curso

Intermediate Regular Expressions in R

Ver curso

Instruções do exercício

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

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# 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 = "___"
)]
Editar e executar o código