Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

r

escaping

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')
like image 801
David LeBauer Avatar asked Apr 09 '12 16:04

David LeBauer


People also ask

What happens if you escape a character in a string?

In short, escaping characters means just ignore the double quotes (mid ones throughout the string).

How do I remove all escape sequences from a string in Python?

You can use regexes to remove the ANSI escape sequences from a string in Python. Simply substitute the escape sequences with an empty string using re. sub(). The regex you can use for removing ANSI escape sequences is: '(\x9B|\x1B\[)[0-?]

What does string escape mean?

Escaping a string means to reduce ambiguity in quotes (and other characters) used in that string. For instance, when you're defining a string, you typically surround it in either double quotes or single quotes: "Hello World."


2 Answers

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.

like image 175
Josh O'Brien Avatar answered Oct 26 '22 18:10

Josh O'Brien


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

like image 26
user1617979 Avatar answered Oct 26 '22 18:10

user1617979