R/escape.R

Defines functions re_escape

Documented in re_escape

#' @title Escape special characters
#'
#' @description `re_escape` escapes all special characters in a string. This function is useful when you want to treat a
#' string literally in a regular expression context, escaping characters that would otherwise be interpreted as special
#' regex operators.
#'
#' @param pattern A character vector where each element is a string in which special regex characters are to be escaped.
#' @return A character vector of the same length as `pattern`.
#' @examples
#' re_escape("a[bc].*d?")
#' re_escape(".^$|*+?{}[]()")
#' @seealso [Python re.escape() documentation](https://docs.python.org/3/library/re.html#re.escape)
#' @export
re_escape <- function(pattern) {
  stringi::stri_replace_all_regex(
    pattern,
    "([.^$\\\\|*+?{}\\[\\]()])",
    "\\\\$1"
  )
}

Try the re package in your browser

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

re documentation built on Sept. 11, 2024, 5:36 p.m.