[R] Escaping . in regular expression
Prof. John C Nash
nashjc at uottawa.ca
Mon Sep 14 21:25:59 CEST 2009
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
More information about the R-help
mailing list