amonk
amonk

Reputation: 1795

escape all punctuation characters in R

Consider having the string: string<- "The qu!ck, brown fox jumps over thelazyd*g!" and you wish to escape all punctuation. Thus:

escaped_string<-"The qu\\!ck\\, brown fox jumps over the \\`lazy\\` d\\*g" 

I have unsuccessfully tried:

>gsub(pattern = "[[:punct:]]",replacement="\\1",string)
[1] "The quck brown fox jumps over the lazy dg

Thoughts? Ideas?

Upvotes: 2

Views: 667

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

You need to use a capturing group and use

string <- "The qu!ck, brown fox jumps over the `lazy` d*g!"
gsub(pattern = "([[:punct:]])",replacement="\\\\\\1",string)
[1] "The qu\\!ck\\, brown fox jumps over the \\`lazy\\` d\\*g\\!"

See the online R demo

The pair of unescaped parentheses in ([[:punct:]]) creates a capturing group that you may access with \1 backreference from the replacement pattern. The replacement pattern should be defined as "\\\\\\1", since to replace with a literal backslash you need to escape the literal backslash (thus, "\\\\" denotes one replacing backslash) and the backreference should be defined with "\\1" (also needs a literal backslash).

Upvotes: 4

Related Questions