russellpierce
russellpierce

Reputation: 4711

In R how can you tell if a string includes escape sequences?

I have a string in R, e.g. x <- "c:\tmp\rest.zip". How can I detect that it has escape sequences in it, vis. \t and \r? Us DOS/Windows guys have a habit of using backslashes that R doesn't like and I'm writing a function where I would like to be able to protect the user from themselves.

Thanks.

Upvotes: 6

Views: 183

Answers (1)

IRTFM
IRTFM

Reputation: 263362

Doubling of the back-slashes in the grep pattern is the path to success:

 xtxt <- c("test\n", "of\t", "escapes")
 grep("\\n|\\t", xtxt)
# [1] 1 2

Another way to be to search for control characters:

 grep("[[:cntrl:]]", xtxt)
#[1] 1 2

Upvotes: 12

Related Questions