List of files starting with a specific character

I have a folder that contains files with names "5_45name.Rdata"and "15_45name.Rdata".

I would like to specify only those starting with "5", in the example above means that I would like to exclude "15_45name.Rdata".

Usage list.files(pattern="5_*name.Rdata"), however, will list both of these files.

Is there a way to tell list.files () that I want the file name to start with a specific character?

+4
source share
1 answer

We need to use the metacharacter ( ^) to indicate the beginning of a line followed by the number 5. Thus, it can have a more specific pattern, as shown below

list.files(pattern ="^5[0-9]*_[0-9]*name.Rdata")

, _ , .

list.files(pattern = "^5.*name.Rdata")
+3

Source: https://habr.com/ru/post/1664281/


All Articles