'Subset according to patterns in file names

I have the following file names in a folder:

1_myfile.txt, 2_myfile.txt, 3_myfile.txt, and 4_best_myfile.txt, 5_best_myfile.txt, 6_best_myfile.txt.

I would like to use regex in pattern = "" when listing files with list.files() in order to subset files containing "_myfile.txt" from files containing "_best_myfile.txt". I tried using:

 files = list.files(path = ".", "*[^best_myfile.txt]$") 

Unfortunately it does not work because it subsets only files that do not end with .txt. How can I solve this?



Solution 1:[1]

We can modify the pattern to "\\d+_best_myfile\\.txt"

files <- list.files("\\d+_best_myfile\\.txt") 

It implies one or more numbers (\\d+) followed by a _ and the string best_myfile.txt. Also, note that some characters needs to be escaped i.e. . is a metacharacter and it implies any character. So, to get the literal dot character, we need to escape (\\) it

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 akrun