Hi, apologies if this is too simple but I've been stuck on the following for a while: I have a vector of strings: filenames with a name before the extension and a variety of possible extensions I want to select only those files with: 1) a ".tab" extension AND 2) the character sequence "lad" anywhere in the name of the file before the extension. Surely this won't take long to do, I thought. (But I was wrong.) What's the regexp pattern to specify here? Thanks, Jon Minton [[alternative HTML version deleted]]
I think this does the trick. Note that it is case sensitive.
> x <- c("lad.tab", "xxladyy.tab",
"xxyy.tab", "lad.tabx", "LAD.tab",
"lad.TAB")
> grep("lad.*\\.tab$", x, value=T)
[1] "lad.tab" "xxladyy.tab"
>
Jon Minton wrote:> Hi, apologies if this is too simple but I've been stuck on the
following for
> a while:
>
>
>
> I have a vector of strings: filenames with a name before the extension and
a
> variety of possible extensions
>
>
>
> I want to select only those files with:
>
> 1) a ".tab" extension
>
> AND
>
> 2) the character sequence "lad" anywhere in the name of the file
before the
> extension.
>
>
>
> Surely this won't take long to do, I thought. (But I was wrong.)
>
>
>
> What's the regexp pattern to specify here?
>
>
>
> Thanks,
>
>
>
> Jon Minton
>
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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
> and provide commented, minimal, self-contained, reproducible code.
>
You might want to try 'glob2rx()', as in
grep(glob2rx("*.tab"), x)
where 'x' is a vector of strings.
-roger
Jon Minton wrote:> Hi, apologies if this is too simple but I've been stuck on the
following for
> a while:
>
>
>
> I have a vector of strings: filenames with a name before the extension and
a
> variety of possible extensions
>
>
>
> I want to select only those files with:
>
> 1) a ".tab" extension
>
> AND
>
> 2) the character sequence "lad" anywhere in the name of the file
before the
> extension.
>
>
>
> Surely this won't take long to do, I thought. (But I was wrong.)
>
>
>
> What's the regexp pattern to specify here?
>
>
>
> Thanks,
>
>
>
> Jon Minton
>
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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
> and provide commented, minimal, self-contained, reproducible code.
>
--
Roger D. Peng | http://www.biostat.jhsph.edu/~rpeng/