从字符串中删除转义,或者“我怎么能得到\\”的方式? [英] Remove escapes from a string, or, "how can I get \ out of the way?"

查看:126
本文介绍了从字符串中删除转义,或者“我怎么能得到\\”的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

逃脱字符在R中引起很多麻烦,如以前的问题所证明的:


  1. 更改列中的值

  2. Can R paste()输出\?

  3. 使用R中的双引号替换转义的双引号

  4. 如何gsub('%','\% ',...在R?

以前的许多问题可以简化为特殊情况如何我有办法吗?



有没有一个简单的方法呢?



例如,我找不到 gsub 的参数,将会从fo中删除所有转义l

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


解决方案

这里的困难是\1 ,虽然它印有两个字形,实际上,在R的视图中是一个单一的字符。事实上,它与\001\01

  nchar(\1)
#[1] 1
nchar(\001 )
#[1] 1
same(\1,\001)
#[1] TRUE
pre>

所以,你可以一般会删除所有的反斜杠,如下所示:

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

但是,如您所见,\1\01 \001将全部呈现为 001 ,(自从他们是所有的不同的名称\001)。






编辑更多关于在转义序列中使用\,以及可以使用它们表示的各种各样的字符(包括Joshua Ulrich所提及的不允许的nul字符串上面的评论),请参阅本节 R语言定义。


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')

解决方案

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.

这篇关于从字符串中删除转义,或者“我怎么能得到\\”的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆