Skip to contents

Escapes the characters that have a meaning in regular expressions, so that a string (a name, a title, a file prefix) can be searched literally inside a larger pattern.

Usage

escape_regex(x)

Arguments

x

Character.

Value

A character vector of the same length.

Details

The copies of this function in the author's projects (escaping_regex()) did not escape the backslash; this one does.

Examples

escape_regex(c("U.S. (1990)", "a+b", "C:\\data"))
#> [1] "U\\.S\\. \\(1990\\)" "a\\+b"               "C:\\\\data"         
grepl(escape_regex("U.S."), c("U.S. economy", "US economy"))
#> [1]  TRUE FALSE