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.
이 연습은 강의의 일부입니다
String Manipulation with stringr in R
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Match Jeffrey or Geoffrey
whole_names <- ___
str_view(boy_names, pattern = ___, match = TRUE)