Home > Enterprise >  R: matching pattern with leading period
R: matching pattern with leading period

Time:05-24

I'm using R's list.files function to pull the full name of a file who's name matches the following actual characters

   .test_

and has a file extension of xlsx (the full file name is .test_2022-05-23.xlsx).

When I execute the following syntax

des <- list.files(path = raw, pattern = "\\.test_.*\\.xlsx$")

, des resolves as character(0).

Apparently the leading

\\.

is not correct?

If I remove the leading period from the actual file's name and execute the following command

des <- list.files(path = raw, pattern = "test_.*\\.xlsx$")

, des resolves to the expected

test_2022-05-23.xlsx

.

What should I set pattern equal to to match the file's name when the name begins with a period?

Thanks for your help,

Bill

CodePudding user response:

You should be able to set all.files=T like this:

des <- list.files(path = raw, pattern = "\\.test_.*\\.xlsx$", all.files=T)
  •  Tags:  
  • r
  • Related