1

I'm trying to use regular expression in a sub() function in order to replace all the "\" in a Vector

I've tried a number of different ways to get R to recognize the "\":

  • I've tried "\\\" but I keep getting errors.
  • I've tried "\.*"
  • I've tried "\\\.*"
data.frame1$vector4 <- sub(pattern = "\\\", replace = ", data.frame1$vector4)

The \ that I am trying to get rid of only appears occasionally in the vector and always in the middle of the string. I want to get rid of it and all the characters that follow it.

The error that I am getting

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

Also I'm struggling to get Stack to print the "\" that I am typing above. It keeps deleting them.

M--
  • 25,431
  • 8
  • 61
  • 93
Sengen
  • 13
  • 2

1 Answers1

0

1) 4 backslashes To insert backslash into an R literal string use a double backslash; however, a backslash is a metacharacter for a regular expression so it must be escaped by prefacing it with another backslash which also has to be doubled. Thus using 4 backslashes will be needed in the regular expression.

s <- "a\\b\\c"
nchar(s)
## [1] 5
gsub("\\\\", "", s)
## [1] "abc"

2) character class Another way to effectively escape it is to surround it with [...]

gsub("[\\]", "", s)
## [1] "abc"

3) fixed argument Perhaps the simplest way is to use fixed=TRUE in which case special characters will not be regarded as regular expression metacharacters.

gsub("\\", "", s, fixed = TRUE)
## [1] "abc"
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • thank you. so to remove a single backslash with sub(), I should use 4 backslashes? cuz I tried using 3 and that didn't work, although that might not have been clear in what I typed above. – Sengen Aug 02 '19 at 01:51
  • cuz when I type in 3 backslashes, Stack seems to remove one every time.... – Sengen Aug 02 '19 at 01:56
  • one of the character strings im trying to clean up looks like this: "NO\xa0" – Sengen Aug 02 '19 at 01:58
  • If you want to remove everything that is not a letter or number then `gsub("[^[:alnum:]]", "", x)` – G. Grothendieck Aug 02 '19 at 02:19