I want to find any files that start with one of the possible prefixes and ends with one of the possible suffixes.
Here is the example:
filesPattern = "[DB|API|S3]_.*.[JSON|SQL|TXT]$"
LIST_OF_FILES = toupper(list.files(dirProcess,
pattern = filesPattern,
ignore.case = T))
 "API_GPT.TXT" "API_GPTR.R" "DB_COUNTRY.SQL"
 "DB_DECISIONS.SQL" "S3_BUCKET_LIST.R"
[DB|API|S3] is a bracket expression that matches a single char:
You may use
filesPattern = "^(DB|API|S3)_.*\\.(JSON|SQL|TXT)$" LIST_OF_FILES = list.files(dirProcess, pattern = filesPattern, ignore.case = TRUE)
^- start of string
(DB|API|S3)- either of the 3 alternatives (
_- an underscore
.*- any 0+ chars, as many as possible
\\.- a literal
(JSON|SQL|TXT)- either of the 3 alternatives (
$- end of string.
You do not need
toupper() as you are using
ignore.case = TRUE argument that makes pattern matching case insensitive.