How do I extract a file/folder_name only from a path?

ぐ巨炮叔叔 提交于 2019-12-01 03:45:48

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!