R/regex.R

#'@title Regular expression usage with sensible defaults
#'@description you want regular expressions to be accurate and fast. This
#'usually translates to either (perl=TRUE, useBytes=TRUE, ignore.case=TRUE)
#'or just fixed=TRUE. Why bother having to type those in every time?
#'
#'Also, let's be honest, gsub is normally used to get rid of substrings
#'you don't want, with replacement = "" - so let's have a function that
#'just does that.
#'
#'@param x the vector to run the regex over looking for matches
#'
#'@param pattern the regular expression to use.
#'
#'@param ignore_case whether to ignore the casing of x when comparing to the pattern or not.
#'
#'@param replacement a string to replace matches with.
#'
#'@name regex
#'@rdname regex
#'
#'@export
fast_grepl <- function(x, pattern, ignore_case = TRUE){
  grepl(x = x, pattern = pattern, useBytes = TRUE, perl = TRUE, ignore.case = ignore_case)
}

#'@rdname regex
#'@export
fixed_grepl <- function(x, pattern){
  grepl(x = x, pattern = pattern, fixed = TRUE)
}

#'@rdname regex
#'@export
fast_remove <- function(x, pattern, ignore_case = TRUE){
  gsub(x = x, pattern = pattern, useBytes = TRUE, perl = TRUE, ignore.case = ignore_case, replacement = "")
}

#'@rdname regex
#'@export
fixed_remove <- function(x, pattern, ignore_case = TRUE){
  gsub(x = x, pattern = pattern, fixed = TRUE, replacement = "")
}

#'@rdname regex
#'@export
fast_substitute <- function(x, pattern, replacement, ignore_case = TRUE){
  gsub(x = x, pattern = pattern, useBytes = TRUE, perl = TRUE, ignore.case = ignore_case, replacement = replacement)
}

#'@rdname regex
#'@export
fixed_substitute <- function(x, pattern, replacement){
  gsub(x = x, pattern = pattern, fixed = TRUE, replacement = replacement)
}
Ironholds/olivr documentation built on May 7, 2019, 6:40 a.m.