[R] Doubt about pattern

Peter J. Acklam pjacklam at online.no
Fri Jan 30 12:46:44 CET 2004

Marcelo Luiz de Laia wrote:

> I have a very simple problem. I have several files in a same
> directory. I would like to send for an object only the files
> that finish in ".sens.". I execute the command below,
> > files <- dir(pattern="*.sens")

That's not even a valid regular expression in most applications,
but "dir" does allow it, for some reason.  Anyway, I think you
are mixing globs and regular expressions.  If "*.sens" is a glob,
then it will find all files ending with ".sens", which is not
the same as ".sens." which you said above.

Stefano Calza <stecalza at tiscali.it> wrote:
> dir(pattern="\\.sens+$")

That will find files ending with ".sens", ".senss", ".sensss",

To find files ending with ".sens.", the regex is

    dir(pattern = "\\.sens\\.$")

If there is a file called just ".sens.", you have to use
"all.files = TRUE", since files beginning with a "." are


Peter J. Acklam - pjacklam at online.no - http://home.online.no/~pjacklam

More information about the R-help mailing list