Special character classes

Commonly used character classes with their own shortcut:

  • \. matches any character apart from a newline.
  • \d matches any digit.
  • \D matches anything that isn’t a digit.
  • \s matches any whitespace (e.g., space, tab, newline).
  • \S matches anything that isn’t whitespace.
  • \w matches any “word” character, i.e. letters and numbers.
  • \W matches any “non-word” character.
x <- "abcd ABCD 12345 -!@#%."
str_view(x, "\\d+")
## [1] │ abcd ABCD <12345> -!@#%.
str_view(x, "\\D+")
## [1] │ <abcd ABCD >12345< -!@#%.>
str_view(x, "\\s+")
## [1] │ abcd< >ABCD< >12345< >-!@#%.
str_view(x, "\\S+")
## [1] │ <abcd> <ABCD> <12345> <-!@#%.>
str_view(x, "\\w+")
## [1] │ <abcd> <ABCD> <12345> -!@#%.
str_view(x, "\\W+")
## [1] │ abcd< >ABCD< >12345< -!@#%.>