Get startedGet started for free

This or that

Searching one word is easy, right? But searching exactly two or three words, you could not do that with a plain old "Control + F" search. But with regular expressions you are now able to define a search pattern that achieves this. You can use the str_view() to see what your regular expression matches.

When you connect multiple words with a pipe operator | you will match both the thing that comes before the pipe and the thing after. And you're not limited to just two. You can also have three options connected with two pipes Hello Anna|Berta|Colin.

You can furthermore use parentheses to group certain words together, looking e.g. for Hello (Anna|Berta|Colin) will produce a different result than the pattern above. Try out both options and compare the results.

This exercise is part of the course

Intermediate Regular Expressions in R

View Course

Exercise instructions

  • Create a pattern that searches for movies starting with "Finding " and followed by the words "Nemo", "Harmony" or "Dory".
  • Now create the same pattern but wrap the three possibilities in parentheses () to compare the results.
  • Choose the one of the two patterns that matches the full movie names "Finding Nemo", "Finding Harmony" and "Finding Dory" and pass it to the third str_match() call.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Append the three options: Match Nemo, Harmony or Dory
str_view(lines, pattern = "Finding ___")

# Wrap the three options in parentheses and compare the results
str_view(lines, pattern = "Finding ___")

# Use the pattern from above that matched the whole movie names
str_match(lines, pattern = "Finding ___")
Edit and Run Code