regescape {utilitiesR}R Documentation

Escapes a string to be regex-safe.

Description

Escapes a string to be regex-safe.

Usage

  regescape(x, escape='.+*?^$[]\\()\{\}|-')

Arguments

x

the string (or character vector of strings) to escape

escape

characters to escape, default '.*?+^$[]\\{}()|-' (the double-backslash is interpreted as a single blackslash by R).

Value

the escaped string. By default the characters '.*?+^$[]\{}()|-' are all escaped.

Note

Remember that R strings require escaping of backslashes too (as well as regex), so for example to escape the actual string '\' which is R string '\\', you would get '\\\\'.

Examples

regescape(c('Hi.', '**hello?**', 'C:\\')) # C:\
# c('Hi\\.',    '\\*\\*hello\\?\\*\\*',    'C:\\\\')

regescape('Hi...?', escape='?') # only escape the '?'
# 'Hi...\\?'

[Package utilitiesR version 2.0 Index]