Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove everything in string up to last forward slash [duplicate]

Tags:

regex

r

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

like image 616
Cybernetic Avatar asked Nov 28 '22 08:11

Cybernetic


1 Answers

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").

like image 141
Rich Scriven Avatar answered Dec 06 '22 15:12

Rich Scriven