Re: [R] Backslash

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Tue 24 May 2005 - 01:49:01 EST

Josef Eschgfaeller wrote:
> Why sometimes one has to put a double
> backslash in regular expressions, but
> often simple backslashes work too?
> Is only a \ required for giving a
> metacharacter its usual meaning?

The general reason is that both R and grep use \ as an escape character.   If you want to send an escape to grep you need to escape the escape in R.

> ---------------------------------------
> u=grep('\\{[\\-u]x',a,perl=T)
>
> # equivalent to
>
> u=grep('\{[\-u]x',a,perl=T)

No. The first one passes the string containing "\{[\-u]x" to grep (after processing the escapes). The second one passes "{[-u]x" to grep.
>
> # but
>
> u=grep('\w',a,perl=T)

\w has no special meaning in R, so this pattern becomes "w".
>
> # is not correct and requires
>
> u=grep('\\w',a,perl=T)

This is the pattern "\w". I don't know if this has special meaning to pcre, but I guess it must, or you wouldn't have tried it.

Duncan Murdoch

> ---------------------------------------
> Josef Eschgfäller
>
>
>
> ------------------------------------------------------------------------
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue May 24 02:04:33 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:00 EST