I would like to use gsub
to replace every occurrence of a backslash in a string with 2 backslashes.
Currently, what I have I tried is gsub("\\\\", "\\", x)
. This doesn't seem to work though. However, if I change the expression to instead replace each backslash with "a", it works fine.
> gsub("\\\\", "\\", "\\") [1] "" > gsub("\\\\", "a", "\\") [1] "a" > gsub("\\\\", "\\\\", "\\") [1] "\\"
The last character is only a single backslash; R just prints 2 because it prints escaped characters with the backslash. Using nchar
confirms that the length is 1.
What causes this functionality? The second argument to gsub
isn't a regular expression, so having 4 backslashes in the string literal should be converted to a character with 2 backslashes. It makes even less sense that the first gsub
call above returns an empty string.
The backslash suppresses the special meaning of the character it precedes, and turns it into an ordinary character. To insert a backslash into your regular expression pattern, use a double backslash ('\\').
The \r metacharacter matches carriage return characters.
Use Slice Operator To Remove Trailing Slash Another way to remove the trailing slash character from a string is to use the slice operator. If you know a string ends with the trailing slash character then you can use my_string[:-1] and this will remove the last character from the string.
Here's what you need:
gsub("\\\\", "\\\\\\\\", "\\") [1] "\\\\"
The reason that you need four backslashes to represent one literal backslash is that "\"
is an escape character in both R strings and for the regex engine to which you're ultimately passing your patterns. If you were talking directly to the regex engine, you'd use "\\"
to indicate a literal backslash. But in order to get R to pass "\\"
on to the regex engine, you need to type "\\\\"
.
(If you are just wanting to double backslashes, you might want to use this instead):
gsub("\\", "\\\\", "\\", fixed=TRUE) [1] "\\\\"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With