Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 用一对印刷品替换或删除多个反斜杠_R_Stringi - Fatal编程技术网

R 用一对印刷品替换或删除多个反斜杠

R 用一对印刷品替换或删除多个反斜杠,r,stringi,R,Stringi,如何用一个反斜杠替换多个反斜杠?我知道在字符串中,单个反斜杠用\\表示,如下所示: nchar('\\') [1] 1 所以我想用一个(打印为两个)替换这个字符串中的所有反斜杠:'thre\\\\fd',当用cat包装时,将产生:thre\fd。我认为stringi软件包有一种方法可以轻松做到这一点,但不知道如何实现 MWE(输出不正确) 期望散射输出 如果所有字符串的斜杠数相同,则这是一个非常简单的gsub: x <- "test\\\\123" gsub("\\\\","\",x)

如何用一个反斜杠替换多个反斜杠?我知道在字符串中,单个反斜杠用
\\
表示,如下所示:

nchar('\\')
[1] 1
所以我想用一个(打印为两个)替换这个字符串中的所有反斜杠:
'thre\\\\fd'
,当用cat包装时,将产生:
thre\fd
。我认为stringi软件包有一种方法可以轻松做到这一点,但不知道如何实现

MWE(输出不正确) 期望散射输出
如果所有字符串的斜杠数相同,则这是一个非常简单的gsub:

x <- "test\\\\123"
gsub("\\\\","\",x)
output: "test\123"

x使用
fixed=TRUE
参数,我们得到

cat(gsub('\\\\', '\\', 'thre\\\\fd', fixed = TRUE), '\n')
#thre\fd 

cat(gsub('\\\\\\', '\\\\', 'thre\\\\\\fd', fixed = TRUE), '\n')
#thre\\fd 

使用
fixed=TRUE
将获得输出,即
cat(gsub(“\\\\\”,“\\\”,“thre\\\\fd',fixed=TRUE))\thre\fd
尝试您的代码……它不能像解析器那样在替换中转义双引号。
x <- "test\\\\123"
gsub("\\\\","\",x)
output: "test\123"
cat(gsub('\\\\', '\\', 'thre\\\\fd', fixed = TRUE), '\n')
#thre\fd 

cat(gsub('\\\\\\', '\\\\', 'thre\\\\\\fd', fixed = TRUE), '\n')
#thre\\fd