2

I have a list of filenames like this:

datalist

EU_AU_abc100.dat
EU_AU_abc200.dat
EU_AU_abc300.dat
EU_AU_abc.dat

I want to have this:

abc100
abc200
abc300
abc

How can I do that? I know the substr(datalist, start = 6, stop = 12) for example, but that doesnt suit for the last example. Any idea how to solve this? Can I use the .dat as stop-marker somehow?

Mr.Spock
  • 511
  • 2
  • 13

1 Answers1

3

Using gsub

gsub('.*_([^_]+)\\.dat','\\1',c("datalist EU_AU_abc100.dat","EU_AU_abc.dat"))
[1] "abc100" "abc" 

.*_ any character followed by a _
([^_]+) capture any character except a _ between .*_ and .dat as group 1. Then returns this group \\1

A. Suliman
  • 12,923
  • 5
  • 24
  • 37