agerom agerom - 1 year ago 46
R Question

escape all punctuation characters in R

Consider having the string:

string<- "The qu!ck, brown fox jumps over the
lazy
d*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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download