How to extract file / folder_name from path only?

Sorry, I suck at regexp. If I have a path like this:

/long/path/to/fileI just need to specify file.

If someone delivers file/, I just need to file.

If someone delivers /file/, I just need to just file.

I used the functions stringras a crutch, but it looks like a straightforward territory grep. Help me please?

+3
source share
2 answers

If I understand correctly, you can use the function basename.

f <- "/long/path/to/file"
basename(f)
# [1] "file"
+7
source

How about this?

> path <- "/long/path/to/file"
> require(stringr)
> str_extract(path, "[^/]*$")
[1] "file"
+2
source

All Articles