Learn R Programming

rex (version 0.1.0)

character_class: Create character classes

Description

There are multiple ways you can define a character class.

Usage

character_class(x)

one_of(...)

any_of(...)

some_of(...)

none_of(...)

except_any_of(...)

except_some_of(...)

range(start, end)

exclude_range(start, end)

Arguments

x
text to include in the character class (must be escaped manually)
...
shortcuts, R variables, text, or other rex functions.
start
beginning of character class
end
end of character class

Functions

  • character_class: explicitly define a character class
  • one_of: matches one of the specified characters.
  • any_of: matches zero or more of the specified characters.
  • some_of: matches one or more of the specified characters.
  • none_of: matches anything but one of the specified characters.
  • except_any_of: matches zero or more of anything but the specified characters.
  • except_some_of: matches one or more of anything but the specified characters.
  • range: matches one of any of the characters in the range.
  • exclude_range: matches one of any of the characters except those in the range.

See Also

Other rex: ., capture, capture_group; %if_next_is%, %if_next_isnt%, %if_prev_is%, %if_prev_isnt%, lookarounds; %or%, or; at_least, at_most, between, counts, n, n_times; group; maybe, one_or_more, wildcards, zero_or_more, zero_or_one; not; rex, rex_; shortcuts

Examples

Run this code
# grey = gray
re <- rex("gr", one_of("a", "e"), "y")
grepl(re, c("grey", "gray")) # TRUE TRUE

# Match non-vowels
re <- rex(none_of("a", "e", "i", "o", "u"))
# They can also be in the same string
re <- rex(none_of("aeiou"))
grepl(re, c("k", "l", "e")) # TRUE TRUE FALSE

# Match range
re <- rex(range("a", "e"))
grepl(re, c("b", "d", "f")) # TRUE TRUE FALSE

# Explicit creation
re <- rex(character_class("abcd\\["))
grepl(re, c("a", "d", "[", "]")) # TRUE TRUE TRUE FALSE

Run the code above in your browser using DataLab