Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

String Manipulation with stringr in R

Cursus bekijken

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Create character class containing vowels
vowels <- ___

# Print vowels
___

# See vowels in x with str_view()
___
Code bewerken en uitvoeren