From simon.urbanek at r-project.org  Mon Apr 24 16:29:43 2006
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Mon, 24 Apr 2006 10:29:43 -0400
Subject: [Rd] gsub + backslashes
In-Reply-To: <Pine.LNX.4.64.0604241620370.12055@imbe153.imbe.med.uni-erlangen.de>
References: <Pine.LNX.4.64.0604241620370.12055@imbe153.imbe.med.uni-erlangen.de>
Message-ID: <22AE17D0-2FCC-466A-8377-84ABE617C500@r-project.org>


On Apr 24, 2006, at 10:23 AM, Torsten Hothorn wrote:

>
> Dear developeRs,
>
> I thought that backslashes can be escaped in the usual way (and I  
> think I
> did this before) but I can't see why
>
> R> gsub("\\", "x", "\alpha")
> Error in gsub(pattern, replacement, x, ignore.case, extended, fixed,
> useBytes) :
>          invalid regular expression '\'
>
> gives an error. Or am I just blind?
>

In REs you need to escape backslashes, so you need double-escaping:  
one for the R string representation and one for the RE:

 > gsub("\\\\", "x", "\\alpha")
[1] "xalpha"

For illustration purposes:

 > cat("\\\\")
\\
 > cat("\alpha")
lpha
 > cat("\\alpha")
\alpha

Cheers,
Simon



[Rd] gsub + backslashes
Simon Urbanek