[R] matching period with perl regular expression
ggrothendieck at gmail.com
Thu May 14 01:55:22 CEST 2009
R interprets backslash to give special meaning to the next character, i.e.
it strips off the backslash and send the following character to gsub
possibly reinterpreting it specially (for example \n is newline). Thus
a backslash will never get to gsub unless you use a double backslash.
Thus we can use "'\\." to represent \. Also note that
that the regular expression "[.]" represents a literal dot and does not
require a backslash in the first place. You don't need perl = TRUE for
simple regular expressions like this.
On Wed, May 13, 2009 at 7:41 PM, Stephen J. Barr <stephenjbarr at gmail.com> wrote:
> I have several strings where I am trying to eliminate the period and
> everything after the period, using a regular expression. However, I am
> having trouble getting this to work.
>> x = "wa.w"
>> gsub(x, "\..*", "", perl=TRUE)
>  ""
> Warning messages:
> 1: '\.' is an unrecognized escape in a character string
> 2: unrecognized escape removed from "\..*"
> In perl, you can match a single period with \.
> Is this not so even with perl=TRUE. I would like for x to be equal to
>> x = "wa"
> What am I missing here?
> Stephen J. Barr
> University of Washington
> WEB: www.econsteve.com
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help