Character classes
In regular expressions a character class is a way of specifying "match one (and only one) of the following characters". In rebus you can specify the set of allowable characters using the function char_class().
This is another way you could specify an alternate spelling, for example, specifying "a gr followed by, either an a or e, followed by a y":
x <- c("grey sky", "gray elephant")
str_view(x, pattern = "gr" %R% char_class("ae") %R% "y")
A negated character class matches "any single character that isn't one of the following", and in rebus is specified with negated_char_class().
Unlike in other places in a regular expression you don't need to escape characters that might otherwise have a special meaning inside character classes. If you want to match . you can include . directly, e.g. char_class("."). Matching a - is a bit trickier. If you need to do it, just make sure it comes first in the character class.
Den här övningen är en del av kursen
String Manipulation with stringr in R
Interaktiv övning med praktiskt arbete
Testa den här övningen genom att slutföra den här exempelkoden.
# Create character class containing vowels
vowels <- ___
# Print vowels
___
# See vowels in x with str_view()
___