1

I find it difficult to replace "&" with "\&" using R's base gsub() function -

gsub("&", "\&", "A&B")

Gives below error -

Error: '\&' is an unrecognized escape in character string starting ""\&"

Is there any way to achieve this substitution?

Bogaso
  • 2,838
  • 3
  • 24
  • 54

1 Answers1

1

You may use

gsub("&", "\\&", "A&B",fixed=TRUE) # Fixed string replacement
gsub("(&)", "\\\\\\1", "A&B")      # Regex replacement

The fixed string replacement is clear: every & is replaced with a \&. The double \ is used in the string literal to denote a literal \.

In the regex replacement, the & is matched and captured into Group 1. Since a backslash is a special character in the regex replacement pattern, it must be doubled, and - keeping in mind a literal backslash is defined with \\ inside a string literal - we need to use \\\\ in the replacement. The \1 is the backreference to Group 1 value, but again, the \ must be doubled in the string, literal, hence, we use \\1 in there. That is why there are 6 backslashes in a row. You may find more about backslashes problem here.

The result only contains a single backslash, you can easily check that using cat or saving the contents to a text file:

cat(gsub("&", "\\&", "A&B",fixed=TRUE), collapse="\n")
cat(gsub("(&)", "\\\\\\1", "A&B"))

See the R demo online

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • But if I pass this result to another variable that new variable still contains double escape. This is disgustedly confusing – Bogaso May 21 '20 at 17:35
  • 2
    @Bogaso See [here](https://stackoverflow.com/a/28204611/3832970), that **DOES NOT** contain any double backslashes. ***Period***. – Wiktor Stribiżew May 21 '20 at 17:37
  • @Bogaso, function `cat` shows strings as they are, without doubling the backslashes. Try `cat(gsub("&", "\\&", "A&B",fixed=TRUE))`, it will show `A\&B`. – Andrey Shabalin May 21 '20 at 17:59