如何仅从路径中提取文件/文件夹名称? [英] How do I extract a file/folder_name only from a path?

查看:126
本文介绍了如何仅从路径中提取文件/文件夹名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,我吮吸正则表达式。如果我有这样的路径:



/ long / path / to / file ,我只需要提取 file



如果有人提供 file / 需要文件



如果有人提供 / file / ,我仍然需要文件



我一直在使用 stringr 起着拐杖的作用,但这看起来像是直接向上 grep 领土。如果我理解正确的话,你可以使用 basename

解决方案



$ b $ f $ f < - / long / path / to / file
basename(f)
#[1]file


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?

解决方案

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

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

这篇关于如何仅从路径中提取文件/文件夹名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆