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?
Best,
Torsten
R> version
_
platform i686-pc-linux-gnu
arch i686
os linux-gnu
system i686, linux-gnu
status Under development (unstable)
major 2
minor 4.0
year 2006
month 04
day 18
svn rev 37840
language R
version.string R version 2.4.0 Under development (unstable) (2006-04-18
r37840)
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
\ is a special character in regular expressions so you need to
escape it as \\ but putting \\ between quotes give you only \
so you need \\\\ to get \\. Any of these would work:
gsub("\\\\", "x", "\\alpha")
gsub("[\\]", "x", "\\alpha")
sub(".", "x", "\\alpha") # assumes \ is first
character
gsub("\\", "x", "\\alpha", fixed = TRUE)
chartr("\\", "x", "\\alpha")
On 4/24/06, Torsten Hothorn <Torsten.Hothorn at rzmail.uni-erlangen.de>
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?
>
> Best,
>
> Torsten
>
> R> version
> _
> platform i686-pc-linux-gnu
> arch i686
> os linux-gnu
> system i686, linux-gnu
> status Under development (unstable)
> major 2
> minor 4.0
> year 2006
> month 04
> day 18
> svn rev 37840
> language R
> version.string R version 2.4.0 Under development (unstable) (2006-04-18
> r37840)
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
On Mon, 24 Apr 2006, 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?Escape for R and escape for regexp:> gsub("\\\\", "x", "\\alpha")[1] "xalpha" -- 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