I'm working in R and having troubles escaping the backslash. I am using the library stringr
.
install.packages("stringr", repos='http://cran.us.r-project.org')
library("stringr")
I would like to do str = str_replace_all(str, "\", "")
So I tried str = str_replace_all(str, "\\", "")
but it won't work.
What should I do?
The following R code explains how to escape a backslash in a character string. To accomplish this, we have to specify a second backslash in front of the backslash that we want to escape.
Escape character syntaxAn escape sequence contains a backslash (\) symbol followed by one of the escape sequence characters or an octal or hexadecimal number. A hexadecimal escape sequence contains an x followed by one or more hexadecimal digits (0-9, A-F, a-f).
I found a solution that works
str = gsub("([\\])","", str)
Use Hmisc::escapeRegex
and Hmisc::escapeBS
which automatically escapes backslashes and other regex special characters.
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