问题描述
我想用gsub
用2个反斜杠替换字符串中每个出现的反斜杠.
I would like to use gsub
to replace every occurrence of a backslash in a string with 2 backslashes.
当前,我尝试的是gsub("\\\\", "\\", x)
.不过,这似乎不起作用.但是,如果我更改表达式以将每个反斜杠替换为"a",则效果很好.
Currently, what I have I tried is gsub("\\\\", "\\", x)
. This doesn't seem to work though. However, if I change the expression to instead replace each backslash with "a", it works fine.
> gsub("\\\\", "\\", "\\")
[1] ""
> gsub("\\\\", "a", "\\")
[1] "a"
> gsub("\\\\", "\\\\", "\\")
[1] "\\"
最后一个字符只是一个反斜杠; R只打印2,因为它打印带有反斜杠的转义字符.使用nchar
确认长度为1.
The last character is only a single backslash; R just prints 2 because it prints escaped characters with the backslash. Using nchar
confirms that the length is 1.
什么原因导致此功能? gsub
的第二个参数不是正则表达式,因此字符串文字中具有4个反斜杠的字符应转换为具有2个反斜杠的字符.上面的第一个gsub
调用返回空字符串的意义更小.
What causes this functionality? The second argument to gsub
isn't a regular expression, so having 4 backslashes in the string literal should be converted to a character with 2 backslashes. It makes even less sense that the first gsub
call above returns an empty string.
推荐答案
这是您需要的:
gsub("\\\\", "\\\\\\\\", "\\")
[1] "\\\\"
之所以需要四个反斜杠来表示一个文字反斜杠,是因为"\"
既是R字符串中的转义字符,也是正要传递其模式的正则表达式引擎的转义字符.如果您直接与正则表达式引擎对话,则可以使用"\\"
表示文字反斜杠.但是为了使R将"\\"
传递到正则表达式引擎,您需要键入"\\\\"
.
The reason that you need four backslashes to represent one literal backslash is that "\"
is an escape character in both R strings and for the regex engine to which you're ultimately passing your patterns. If you were talking directly to the regex engine, you'd use "\\"
to indicate a literal backslash. But in order to get R to pass "\\"
on to the regex engine, you need to type "\\\\"
.
(如果您只是想将反斜杠加倍,则可能要使用它):
(If you are just wanting to double backslashes, you might want to use this instead):
gsub("\\", "\\\\", "\\", fixed=TRUE)
[1] "\\\\"
这篇关于R-gsub替换反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!