ComenzarEmpieza gratis

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.

Este ejercicio forma parte del curso

String Manipulation with stringr in R

Ver curso

Ejercicio interactivo práctico

Prueba este ejercicio completando el código de muestra.

# Match Jeffrey or Geoffrey
whole_names <- ___
str_view(boy_names, pattern = ___, match = TRUE)
Editar y ejecutar código