Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Backslash headache in str_replace() #394

Open
ksedivyhaley opened this issue Oct 27, 2016 · 6 comments
Open

Backslash headache in str_replace() #394

ksedivyhaley opened this issue Oct 27, 2016 · 6 comments

Comments

@ksedivyhaley
Copy link

ksedivyhaley commented Oct 27, 2016

First off, the text says that you need \ \ \ \ to represent a backslash within a regular expression within a string in R. Presumably this is because the string renders \ as a single , which regex thinks is an escape character without anything to escape from? Then \ \ \ \ is \ \ within regex which does give you escape-\

This seemed to work, in this section I've hit the exercise: "Replace all forward slashes in a string with backslashes."

So I set up my test vector and throw backslashes at it:

test <- c("hello/world", "no slashes here", "/double/slash")
str_replace_all(test, "/", "\")
[1] "hello\world" "no slashes here" "\double\slash"

Did I use too many backslashes?

str_replace_all(test, "/", "")
[1] "helloworld" "no slashes here" "doubleslash"

Add another character.... \ \ \ \ = \ \ but \ \ = nothing?

str_replace_all(test, "/", "!")
[1] "hello!world" "no slashes here" "!double!slash"

...unless the extra character actually needs an escape, in which case I get a backslash back???

str_replace_all(test, "/", "\"")
[1] "hello"world" "no slashes here" ""double"slash"

It looks a bit like it's reading escape--escape-, which would mean that \ \ should give me an actual backslash - but it didn't! And if that's the case then if I move the quote mark so " comes before any further backslashes, I should get 'hello"\world'...

str_replace_all(test, "/", """)
[1] "hello"world" "no slashes here" ""double"slash"

But I don't.

The heck is going on here?

@jennybc
Copy link
Member

jennybc commented Oct 28, 2016

That was a tough one! But I sort of have an answer. I will pursue this because I am still a bit puzzled.

If you just print stuff to screen, it looks like it's not working:

test <- c("hello/world", "no slashes here", "/double/slash")
str_replace_all(test, "/", "\\")
#> "helloworld"      "no slashes here" "doubleslash"    
str_replace_all(test, "/", "\\\\")
#> "hello\\world"    "no slashes here" "\\double\\slash"

But if you write those results to file and look at the files, you'll see the four backslash version is indeed working. I'll get back to you with more explanation.

write(str_replace_all(test, "/", "\\"), "two.txt")
write(str_replace_all(test, "/", "\\\\"), "four.txt")

Contents of two.txt:

helloworld
no slashes here
doubleslash

Contents of four.txt:

hello\world
no slashes here
\double\slash

I suspect this exercise wasn't meant to be this hard.

@jennybc
Copy link
Member

jennybc commented Oct 28, 2016

The presence of the two backslashes in the printed result is the escaping of the backslash, which is necessary in the string. Which we can see more clearly once we store the string to file.

@jennybc
Copy link
Member

jennybc commented Oct 28, 2016

Ok here's a cleaner demo. You can also process the result through cat() or writeLines().

library(stringr)
test <- c("hello/world", "no slashes here", "/double/slash")
str_replace_all(test, "/", "\\\\")
#> [1] "hello\\world"    "no slashes here" "\\double\\slash"
cat(str_replace_all(test, "/", "\\\\"), sep = "\n")
#> hello\world
#> no slashes here
#> \double\slash
writeLines(str_replace_all(test, "/", "\\\\"))
#> hello\world
#> no slashes here
#> \double\slash

@samhinshaw
Copy link

Perhaps the most valuable resource of all: https://xkcd.com/1638/
Perhaps the most valuable resource of all

@ksedivyhaley
Copy link
Author

So "\\\\" is correct and the extra backslash I was seeing was an artifact of the way strings are printed?

I noticed writeLines() being used to check the input in the text, but it hadn't occurred to me to use it to check the output. Thanks!

@jennybc
Copy link
Member

jennybc commented Oct 28, 2016

So "\\\\" is correct and the extra backslash I was seeing was an artifact of the way strings are printed?

Yep!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants