[R] Regex - subsetting parts of a file name.

arun smartpink111 at yahoo.com
Thu Jul 31 17:08:57 CEST 2014

gsub(".*\\.(.*)\\..*","\\1", my.cache.list)
[1] "subject_test"  "subject_train" "y_test"        "y_train" 


str_extract(my.cache.list, perl('(?<=\\.).*(?=\\.)'))
[1] "subject_test"  "subject_train" "y_test"        "y_train"  


On Thursday, July 31, 2014 11:05 AM, arnaud gaboury <arnaud.gaboury at gmail.com> wrote:
A directory is full of data.frames cache files. All these files have
the same pattern:


my.cache.list <- c("df.subject_test.RData", "df.subject_train.RData",

I want to keep only the part inside the two points. After lots of
headache using grep() when trying something like this:


I couldn't find a clean one liner and found this workaround:

my.cache.list <- gsub('df.','',my.cache.list)
my.cache.list <- gsub('.RData','',my.cache.list)

The two above commands do the trick, but a clean one line with some
regex expression would be a more "elegant" way.

Does anyone have any suggestion ?

TY for help

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list