Search code examples
rsubstring

Extract everything string before last bottom line


I have a string like this:

"19970101_AM23_popu_223_AZ1_Country.csv"

I want to extract every string before last underscore:

"19970101_AM23_popu_223_AZ1.csv"

I have tried this:

gsub("_.//*", "", x)

But got the same

"19970101_AM23_popu_223_AZ1_Country.csv"


Solution

  • Try

    gsub("_\\w+$", "", x)