问题
Unfortunately I suck at regexp. If I have a path like so:
/long/path/to/file
, I just need to extact file
.
If someone supplies file/
I just need file
.
If someone supplies /file/
, I still need just file
.
I've been using stringr
functions as a crutch but this seems like straight up grep
territory. Help, please?
回答1:
If I understand correctly, you could use the basename
function.
f <- "/long/path/to/file"
basename(f)
# [1] "file"
回答2:
What about this?
> path <- "/long/path/to/file"
> require(stringr)
> str_extract(path, "[^/]*$")
[1] "file"
来源:https://stackoverflow.com/questions/9693877/how-do-i-extract-a-file-folder-name-only-from-a-path