Aan de slagGa gratis aan de slag

Digits, words and spaces

So far in your life you might have always searched for one number or word exactly. Now you have a much more flexible tool at hand, to search for:

  • \\d digits (zero to nine)
  • \\w word characters (letters, numbers or underscores)
  • \\s white spaces (also tabs and line breaks)

Plus, you can use square brackets [A-Za-z] and have a list of possible values inside.

You already found all sequels of "Saw". Can you create a pattern that matches all sequels in the list movie_titles? They usually have a number at the end, right?

Furthermore, the list contains duplicates introduced by "Grey" (British) and "Gray" (American English). Create a pattern that matches both versions of the color.

Lastly, list out all movie titles that contain special, non word characters.

Deze oefening maakt deel uit van de cursus

Intermediate Regular Expressions in R

Cursus bekijken

Oefeninstructies

  • Match all movies titles that end with a space followed by a digit.
  • Match both "Grey" and "Gray" with a custom pattern […].
  • Write a pattern that matches everything but word characters \\w and spaces \\s.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# List all movies that end with a space and a digit
movie_titles[str_detect(movie_titles,
  pattern = "___"
)]

# List all movies that contain "Grey" or "Gray"
movie_titles[str_detect(movie_titles,
  pattern = "Gr___y"
)]

# List all movies with strange characters (no word or space)
movie_titles[str_detect(movie_titles,
  pattern = "[___]"
)]
Code bewerken en uitvoeren