regescape {utilitiesR} | R Documentation |
Escapes a string to be regex-safe.
regescape(x, escape='.+*?^$[]\\()\{\}|-')
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). |
the escaped string. By default the characters '.*?+^$[]\{}()|-' are all escaped.
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 '\\\\'.
regescape(c('Hi.', '**hello?**', 'C:\\')) # C:\ # c('Hi\\.', '\\*\\*hello\\?\\*\\*', 'C:\\\\') regescape('Hi...?', escape='?') # only escape the '?' # 'Hi...\\?'