Get file name from url in R

I would like to extract the file name from the url in R. Now I am doing it as follows, but maybe this can be done shorter, as in python. assuming the path is just a string.

path="http://www.exanple.com/foo/bar/fooXbar.xls" 

in R:

 tail(strsplit(path,"[/]")[[1]],1) 

in Python:

 path.split("/")[-1:] 

Maybe some solution sub, gsub?

+5
source share
2 answers

There is a function for this ...

 basename(path) [1] "fooXbar.xls" 
+15
source

@ SimonO101 has the most reliable IMO answer, but some other options:

Since regular expressions are greedy, you can take advantage of them

 sub('.*/', '', path) # [1] "fooXbar.xls" 

Also, you do not need [] around / in strsplit .

 > tail(strsplit(path,"/")[[1]],1) [1] "fooXbar.xls" 
+1
source

Source: https://habr.com/ru/post/1502061/


All Articles