Get startedGet started for free

Alternation

The rebus function or() allows us to specify a set of alternatives, which may be single characters or character strings, to be matched. Each alternative is passed as a separate argument.

For example, or("grey", "gray") allows us to detect either the American or British spelling:

x <- c("grey sky", "gray elephant")
str_view(x, pattern = or("grey", "gray"))

Since these two words only differ by one character you could equivalently specify this match with "gr" %R% or("e", "a") %R% "y", that is "a gr followed by, an e or an a, then a y".

Notice we've added the argument match = TRUE to str_view(), this will only display elements that had a match, which is useful when you are searching over many strings.

This exercise is part of the course

String Manipulation with stringr in R

View Course

Hands-on interactive exercise

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

# Match Jeffrey or Geoffrey
whole_names <- ___
str_view(boy_names, pattern = ___, match = TRUE)
Edit and Run Code