Remove escapes from a string, or, “how can I get \ out of the way?”

久未见 提交于 2020-01-22 18:00:02

问题


Escape characters cause a lot of trouble in R, as evidenced by previous questions:

  1. Change the values in a column
  2. Can R paste() output "\"?
  3. Replacing escaped double quotes by double quotes in R
  4. How to gsub('%', '\%', ... in R?

Many of these previous questions could be simplified to special cases of "How can I get \ out of my way?"

Is there a simple way to do this?

For example, I can find no arguments to gsub that will remove all escapes from the following:

 test <- c('\01', '\\001')

回答1:


The difficulty here is that "\1", although it's printed with two glyphs, is actually, in R's view a single character. And in fact, it's the very same character as "\001" and "\01":

nchar("\1")
# [1] 1
nchar("\001")
# [1] 1
identical("\1", "\001")
# [1] TRUE

So, you can in general remove all backslashes with something like this:

(test <- c("\\hi\\", "\n", "\t", "\\1", "\1", "\01", "\001"))
# [1] "\\hi\\" "\n"     "\t"      "\\1"    "\001"   "\001"   "\001"  
eval(parse(text=gsub("\\", "", deparse(test), fixed=TRUE)))
# [1] "hi"  "n"   "t"   "1"   "001" "001" "001"

But, as you can see, "\1", "\01", and \001" will all be rendered as 001, (since to R they are all just different names for "\001").


EDIT: For more on the use of "\" in escape sequences, and on the great variety of characters that can be represented using them (including the disallowed nul string mentioned by Joshua Ulrich in a comment above), see this section of the R language definition.




回答2:


I just faced the same issue - if you want any \x where x is a character then I am not sure how, I wish I knew, but to fix it for a specific escape sequence,. say \n then you can do

new = gsub("\n","",old,fixed=T)

in my case, I only had \n



来源:https://stackoverflow.com/questions/10076507/remove-escapes-from-a-string-or-how-can-i-get-out-of-the-way

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!