[R] Regexp pattern but fixed replacement?

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Thu Apr 11 18:35:31 CEST 2024


I noticed this issue in stringr::str_replace, but it also affects sub() 
in base R.

If the pattern in a call to one of these needs to be a regular 
expression, then backslashes in the replacement text are treated specially.

For example,

   gsub("a|b", "\\", "abcdef")

gives "def", not "\\\\def" as I wanted.  To get the latter, I need to 
escape the replacement backslashes, e.g.

   gsub("a|b", "\\\\", "abcdef")

which gives "\\\\cdef".

I have two questions:

1.  Is there a variant on sub or str_replace which allows the pattern to 
be declared as a regular expression, but the replacement to be declared 
as fixed?

2.  To get what I want, I can double the backslashes in the replacement 
text.  This would do that:

    replacement <- gsub("\\\\", "\\\\\\\\", replacement)

Are there any other special characters to worry about besides backslashes?

Duncan Murdoch



More information about the R-help mailing list