[R] grep and gsub on backslash and quotes

Simon Fear Simon.Fear at synequanon.com
Tue Aug 12 17:46:52 CEST 2003


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? How would I know how many levels of doubling up to use for any
other
functions? (I got to 4 consecutive \ by trial and error in this case,
but
have a dim memory of having read about it somewhere.)

TIA
 

Simon Fear
Senior Statistician
Syne qua non Ltd
Tel: +44 (0) 1379 644449
Fax: +44 (0) 1379 644445
email: Simon.Fear at synequanon.com
web: http://www.synequanon.com
 
Number of attachments included with this message: 0
 
This message (and any associated files) is confidential and\...{{dropped}}




More information about the R-help mailing list