Session Ready
Exercise

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.

Instructions 1/4
undefined XP
  • 1

    Create a pattern that matches Jeffrey or Geoffrey in boy_names. Assign this to whole_names.

    • 2

      Create a pattern that matches Je or Geo followed by ffrey. Assign this to common_ending. You should get the same matches as the previous pattern.

    • 3

      Create a pattern that matches Je or Geo, followed by ff, followed by one of ry, ery rey, or erey. Assign this to by_parts.

    • 4

      Create a pattern that matches all girl_names that start with C or K followed by ath. Assign this to ckath.