[Rd] gsub(replacement="\\X") -> "X", not "\\X"

Bill Dunlap bill at insightful.com
Sat Mar 25 21:49:33 CET 2006


I would have thought that all 4 of the following
would give the same result (the first):
   > gsub("backslash-X", "\\X", "this is a backslash-X", fixed=T)
   [1] "this is a \\X"
   > gsub("backslash-X", "\\X", "this is a backslash-X", extended=F)
   [1] "this is a X"
   > gsub("backslash-X", "\\X", "this is a backslash-X", extended=T)
   [1] "this is a X"
   > gsub("backslash-X", "\\X", "this is a backslash-X", perl=T)
   [1] "this is a X"
If fixed==F the first "\\" is ignored except if it is followed by a
digit.  In particular, the replacement string must use "\\\\" to insert
a backslash and "\\" by itself is the same as "".  When fixed==T,
replacement="\\" means to insert a backslash.

I've seen code in package:gdata that depends on this behavior but I did
not see it described in help(gsub).  It describes using
replacement="\\<digit>" to put a matched subpattern into the output,
but not the fact that replacement="\\<nondigit>" is equivalent to
replacement="<nondigit>" (but only when fixed==F).

Is that intended or is the help file incomplete?  I'd like to
get Splus's gsub/substituteString in line with R's.

----------------------------------------------------------------------------
Bill Dunlap
Insightful Corporation
bill at insightful dot com
360-428-8146

 "All statements in this message represent the opinions of the author and do
 not necessarily reflect Insightful Corporation policy or position."



More information about the R-devel mailing list