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.