[R] Escaping . in regular expression

Prof. John C Nash nashjc at uottawa.ca
Mon Sep 14 21:37:12 CEST 2009


Thanks. That fixes things.

I saw mention of \\. in ?Quotes, but the usage was not clear. I took it 
to mean the \ was to be printed. Maybe my little example would help, as 
?Quotes doesn't have any.

JN

Henrique Dallazuanna wrote:
> You need double backslashes:
> 
> grep('\\.f', cvec, value = TRUE)
> 
> On Mon, Sep 14, 2009 at 4:25 PM, Prof. John C Nash <nashjc at uottawa.ca 
> <mailto:nashjc at uottawa.ca>> wrote:
> 
>     If I run
> 
> 
>     cvec<-c("test.f", "test.sf", "try.g","try.res", "try.f")
>     print(cvec)
>     indx<-grep('\.f',cvec,perl=TRUE)
>     fset<-cvec[indx]
>     print(fset)
> 
>     I get
> 
>      > cvec<-c("test.f", "test.sf", "try.g","try.res", "try.f")
>      > print(cvec)
>     [1] "test.f"  "test.sf" "try.g"   "try.res" "try.f"
>      > indx<-grep("\.f",cvec,perl=TRUE)
>     Warning messages:
>     1: '\.' is an unrecognized escape in a character string
>     2: unrecognized escape removed from "\.f"
>      > fset<-cvec[indx]
>      > print(fset)
>     [1] "test.f"  "test.sf" "try.f"
>      >
> 
>     This ignores the . for which I want to test.
> 
>     In perl, the function
> 
>     #!/usr/bin/perl
>     use strict;
>     my @cvec=("test.f", "test.sf", "try.g","try.res", "try.f");
>     foreach my $elem (@cvec) {
>       print "$elem : ";
>       if ( $elem =~ '\.f' ) {
>           print "matches \n";
>       } else {
>           print "does not match \n";
>       }
>     }
> 
> 
>     gives
> 
>     $ perl perlmatch.pl
>     test.f : matches
>     test.sf : does not match
>     try.g : does not match
>     try.res : does not match
>     try.f : matches
>     $
> 
>     which does what I want. It looks like a bug (or at least a nasty
>     documentation failure) of "perl=TRUE".
> 
>     Anyone have suggestions of how to get appropriate filtering? I need
>     this to automate optimization tests on large sets of test problems.
> 
>     Cheers, JN
> 
>     ______________________________________________
>     R-help at r-project.org <mailto:R-help at r-project.org> mailing list
>     https://stat.ethz.ch/mailman/listinfo/r-help
>     PLEASE do read the posting guide
>     http://www.R-project.org/posting-guide.html
>     and provide commented, minimal, self-contained, reproducible code.
> 
> 
> 
> 
> -- 
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O




More information about the R-help mailing list