[R] grep and gsub on backslash and quotes

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Aug 12 18:13:14 CEST 2003

On Tue, 12 Aug 2003, Simon Fear wrote:

> The following code works,  to gsub single quotes to double quotes:
> line <- gsub("'", '"', line)
> (that's a single quote within doubles then a double within singles if
> your
> viewer's font is not good).
> But The R Language Manual tells me that
> Quotes and other special characters within strings
> are specified using escape sequences:
> \' single quote
> \" double quote
> so why is the following wrong: gsub("\\\\'", "\\\\"", line)? That or any
> other number of backslashes (have tried all up to n=6 just for good
> measure).
> BTW is it documented anywhere that you need four backslashes in an RE to
> match one in the target, when it is being passed as an argument to gsub
> or
> grep?

It's not true, so I hope it is not documented anywhere.  You may need 6, 
as in the following from methods():

    res <- sort(grep(gsub("([.[])", "\\\\\\1", name), an, value = TRUE))

since that is \\ \1 withou tthe space.  Each backslash in the target only
needs to be doubled.

In your example

gsub("\'", "\"", line) or even gsub("'", "\"", line)

is all you need: only R strings need the escape.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list