full.path = 'C:\Users\me\Desktop\Data\my_file.csv'
我无法找出只留下
的正确的正则表达式essential.name = 'my_file'
我担心我继续无法正确编码最后反斜杠
答案 0 :(得分:4)
独立于平台的正则表达式解决方案也可以看起来像
library(ggplot2)
long <- cbind(as.data.frame.table(M, responseName = "cor"), myMat = c(myMat))
ggplot(long, aes(Var1, Var2, col = cor, size = myMat)) +
geom_point() +
scale_colour_gradient(low = "red", high = "blue") +
xlab("") +
ylab("")
请参阅online R demo。
详细:
> full.path = 'C:\\Users\\me\\Desktop\\Data\\my_file.csv'
> sub(".*\\\\([^.]*).*", "\\1", full.path)
[1] "my_file"
- 截至最后的任意数量的0个字符...... .*
- 文字\\\\
符号\
- 第1组捕获0以外的字符([^.]*)
- 其余的角色一直到最后。 .*
只是将第1组的内容插入到结果中。
答案 1 :(得分:2)
我们可以使用basename
和file_path_sans_ext
(来自tools
)来提取文件名
tools::file_path_sans_ext(basename(full.path))
#[1] "my_file"
如果我们需要regex
,请使用gsub
gsub(".*\\\\|\\..*$", "", full.path)
#[1] "my_file"
full.path = 'C:\\Users\\me\\Desktop\\Data\\my_file.csv'