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.
This exercise is part of the course
String Manipulation with stringr in R
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create character class containing vowels
vowels <- ___
# Print vowels
___
# See vowels in x with str_view()
___