I have a string like this:
"vehicles/vehicle_type/filename.csv"
I just want to be left with:
"filename.csv"
I have tried this:
sub('/^(.*[\\\/])/', "", the_string)
But get an "unrecognized escape in character string" error
To grab the end of a file path, you could use simply basename()
.
x <- "vehicles/vehicle_type/filename.csv"
basename(x)
# [1] "filename.csv"
Or if you'd like to continue using regex, adjust your sub()
call to
sub(".*/", "", x)
# [1] "filename.csv"
.*
removes everything, so .*/
removes everything up to and including the final /
(because the previous one was included in the "everything").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With