R/escape_strings.R

Defines functions escape_strings

#' Escape infix regex characters
#'
#' @param x character vector with strings to be used in a regex
#'
#' @return a character vector
#' @noRd
escape_strings <- function(x) {
  strings_to_escape <- c("*", ".", "?", "^", "+", "$", "|", "(", ")", "[",
                         "]", "{", "}", "\\", "-")
  out <- gsub(
    paste0("([\\", paste0(collapse = "\\", strings_to_escape), "])"),
    "\\\\\\1",
    x,
    perl = TRUE)

  return(out)

}

Try the origin package in your browser

Any scripts or data that you put into this service are public.

origin documentation built on April 3, 2025, 11:03 p.m.