quotemeta: Escape a string for use in a Perl-compatible regular...

quotemetaR Documentation

Escape a string for use in a Perl-compatible regular expression

Description

This is an implementation of Perl's quotemeta operator. It is meant for escaping any characters that might have special meanings in a PCRE.

Usage

quotemeta(string)

Arguments

string

The string to escape

Value

string, with all non-word characters escaped with backslashes.

Note that this strategy does not work for R's own regular expression syntax, so you should specify perl = TRUE, or an equivalent option, when using this to construct a regular expression.

Examples


s <- "A (string) with [many] regex *characters* in it."
grepl(s, s, perl = TRUE)
quotemeta(s)
# After escaping, the string matches itself.
grepl(quotemeta(s), s, perl = TRUE)


DarwinAwardWinner/rctutils documentation built on July 22, 2022, 5:19 a.m.