-1

Say you have a vector of characters like this:

x <- c('file_123',
       'file_456',
       'file_clean_67890',
       '123_file_1234')

And you want to extract the end of the string after the last "_". How would you do this? The result I would like is:

"123"   "456"   "67890" "1234" 

If you use str_split, you can't get the very last one because they have different lengths:

> x %>% str_split(pattern = '_', simplify = T)
     [,1]   [,2]    [,3]  
[1,] "file" "123"   ""    
[2,] "file" "456"   ""    
[3,] "file" "clean" "6789"
[4,] "123"  "file"  "1234"

This questions is somewhat duplicate of this one with the difference that since I don't know the number of splits. Also, you could have digit characters before the last underscore, so you can't just detect those.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Leo Barlach
  • 480
  • 3
  • 13

1 Answers1

0

Use str_extract_all from stringr with a regex. See ?regular expression for information on [:digit:], + and $ metacharacters:

str_extract_all(x,"_[[:digit:]]+$")