首页 > 解决方案 > Trying to figure out regular expression in R for sub()

问题描述

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 "\":

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.

标签: r

解决方案


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"

推荐阅读