[R] Escaping . in regular expression

Duncan Murdoch murdoch at stats.uwo.ca
Mon Sep 14 21:36:03 CEST 2009


On 9/14/2009 3:25 PM, Prof. John C Nash 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.

You need to put a \ in the string, and that takes two backslashes. 
You've just been caught by the need to escape the escape to get it there:

 > cvec<-c("test.f", "test.sf", "try.g","try.res", "try.f")
 > indx<-grep("\\.f",cvec,perl=TRUE)
 > indx
[1] 1 5

There have been proposals to have a way to enter strings in R that 
doesn't require \ to be escaped, but so far no agreement on what it 
should look like.

Duncan Murdoch

> 
> 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 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.




More information about the R-help mailing list