Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expressions for finding files

Tags:

regex

r

Ok I'm giving up and ask the question after I read through the help article of regex and still don't have a clue what I'm looking for:

I Have a list of files:

files <- c("files_combined.csv","file_1-10.csv","file_11-20.csv",
           "file_21-30.csv","file_2731-2740.csv","file_2731-2740.txt")

I want only the csv files that start with "file_" and end with ".csv". I know the it looks something like this:

grep(pattern = "^file_???.csv$" ,files)

But I need to find the correct regular expression that ignores the number of characters between the first and the second pattern ("file_" + ".csv"). I'd really appreciate if somebody knows a complete list with the regular expressions in R since it is tedious to read through the help every time and, as in my case not successful, sometimes...

like image 808
JBGruber Avatar asked Mar 14 '23 17:03

JBGruber


1 Answers

R offers a function for doing wildcard expansion using glob patterns for those who don't like regex:

files <- Sys.glob("file_*.csv")

This should match your pattern.

like image 116
doctorG Avatar answered Mar 20 '23 03:03

doctorG