R:如何用*单个*反斜杠和空格('\')替换字符串中的空格('')
我已经搜索了很多次,但在这里或其他地方都没有找到答案.我想用 '\'
替换包含文件名的变量中的每个空格 ' '
.(一个用例可能是 shell 命令,空格被转义,所以每个文件名不会显示为参数列表.)我已经浏览了 StackOverflow 问题 如何替换 R 中的单个反斜杠",并发现许多组合确实如宣传的那样工作:
I've searched many times, and haven't found the answer here or elsewhere. I want to replace each space ' '
in variables containing file names with a '\ '
. (A use case could be for shell commands, with the spaces escaped, so each file name doesn't appear as a list of arguments.) I have looked through the StackOverflow question "how to replace single backslash in R", and find that many combinations do work as advertised:
> gsub(" ", "\\\\", "a b")
[1] "a\\b"
> gsub(" ", "\\ ", "a b", fixed = TRUE)
[1] "a\\ b"
但是用单斜线版本试试这些,R 会忽略它:
but try these with a single-slash version, and R ignores it:
> gsub(" ", "\\ ", "a b")
[1] "a b"
> gsub(" ", "\ ", "a b", fixed = TRUE)
[1] "a b"
对于相反方向的情况——从字符串中删除斜线,它适用于两个:
For the case going in the opposite direction — removing slashes from a string, it works for two:
> gsub("\\\\", " ", "a\\b")
[1] "a b"
> gsub("\\", " ", "a\\b", fixed = TRUE)
[1] "a b"
然而,对于单斜线,R 中的一些内部反常使我什至无法尝试删除它们:
However, for single slashes some inner perversity in R prevents me from even attempting to remove them:
> gsub("\\", " ", "a\\b")
Error in gsub("\\", " ", "a\\b") :
invalid regular expression '\', reason 'Trailing backslash'
> gsub("\", " ", "a\b", fixed = TRUE)
Error: unexpected string constant in "gsub("\", " ", ""
无效的正则表达式"告诉我们一些事情,但我不明白是什么.(还要注意 perl = True
选项没有帮助.)
The 'invalid regular expression' is telling us something, but I don't see what. (Note too that the perl = True
option does not help.)
即使有三个反斜杠,R 也没有注意到一个:
Even with three back slashes R fails to notice even one:
> gsub(" ", "\\\ ", "a b")
[1] "a b"
模式也延伸了!即使是两个工作的倍数:
The patter extends too! Even multiples of two work:
> gsub(" ", "\\\\\\\\", "a b")
[1] "a\\\\b"
但不是奇数倍数(应该得到 '\\\'
:
but not odd multiples (should get '\\\ '
:
> gsub(" ", "\\\\\\ ", "a b")
[1] "a\\ b"
> gsub(" ", "\\\ ", "a b", fixed = TRUE)
[1] "a\\ b"
(我希望有 3 个斜杠,而不是两个.)
(I would expect 3 slashes, not two.)
我的两个问题是:
- 如何实现将
' '
替换为'\ '
的目标? - 为什么替换的奇数斜线变体失败,而偶数斜线替换有效?
对于 shell 命令,一个简单的解决方法是引用文件名,但我的部分兴趣只是想了解 R 的正则表达式引擎发生了什么.
For shell commands a simple work-around is to quote the file names, but part of my interest is just wanting to understand what is going on with R's regex engine.
准备好面对面,因为:
> gsub(" ", "\\\ ", "a b", fixed = TRUE)
[1] "a\\ b"
实际上正在工作.
您看到的两个反斜杠只是 R 控制台显示单个反斜杠的方式,在打印到屏幕时会对其进行转义.
The two backslashes you see are just the R console's way of displaying a single backslash, which is escaped when printed to the screen.
要确认用单个反斜杠替换确实有效,请尝试将输出写入文本文件并检查自己:
To confirm the replacement with a single backslash is indeed working, try writing the output to a text file and inspect yourself:
f <- file("C:\\output.txt")
writeLines(gsub(" ", "\\", "a b", fixed = TRUE), f)
close(f)
在 output.txt
中,您应该看到以下内容:
In output.txt
you should see the following:
a\b