从路径中提取文件夹、文件名和扩展名

Extracting folder, filename and extension from path

我有一个包含文件路径和名称的数据框,格式如下:

files_list <- c(
  "C:/User/Name/Folder/Subfolder1/Sub-subfolder/file.txt", 
  "C:/User/Name/Folder/Subfolder1/Sub-subfolder/file - Copy.txt",
  "C:/User/Name/Folder/Subfolder1/Sub-subfolder/file (1).txt",
  "C:/User/Name/Folder/Subfolder1/Sub-subfolder/file - Copy (2).txt",
  "C:/User/Name/Folder/Subfolder1/fileB.txt",
  "C:/User/Name/Folder/file.C.txt",
  "C:/User/Name/Folder/file-D.txt", 
  "C:/User/Name/Folder/file",
  "C:/User/Name/Folder/file Z.txt", 
  "C:/User/Name/Folder/file - backup.txt"
)

每个文件都有一个父文件夹和一个名称。这些名称可能包含一个或多个句点“.”。 and/or 破折号“-”。此外,有些具有 "Copy" 符号、数字名称、and/or 文件扩展名。我想将数据转换成如下所示:

[1]  "Sub-subfolder   file   txt"
[2]  "Sub-subfolder   file   Copy   txt"
[3]  "Sub-subfolder   file   1   txt"
[4]  "Sub-subfolder   file   Copy   2   txt"
[5]  "Subfolder1   fileB   txt"
[6]  "Folder   file.C   txt"
[7]  "Folder   file-D   txt"
[8]  "Folder   file"
[9]  "Folder   file Z   txt"
[10] "Folder   file - backup   txt"

这是我认为应该解决问题的代码:

sub(
  "(^.:/)([^/.]+/)*([^/.]+/)([^/]+)(\s-\sCopy)?(\s\(([0-9]+)\))?(\.([^.]+))?$", 
  "\3   \4   \5   \7   \9",
  files_list
)

但我得到的是:

[1] "Sub-subfolder/   file.txt         "           
[2] "Sub-subfolder/   file - Copy.txt         "    
[3] "Sub-subfolder/   file (1).txt         "       
[4] "Sub-subfolder/   file - Copy (2).txt         "
[5] "Subfolder1/   fileB.txt         "             
[6] "Folder/   file.C.txt         "                
[7] "Folder/   file-D.txt         "                

我可以处理斜线“/”和额外的空格,但是 "Copy" 符号、数字名称和文件扩展名没有像我期望的那样被分开。

关于如何识别 "Copy" 符号、编号名称和文件扩展名,有什么建议吗?或者我应该只在一行代码中识别父文件夹并将其余的分开在另一行中?

(最终,我会将这些文本字符串转换为数据框,其中文件夹、文件名、副本名称和扩展名是单独的列。我很确定我可以用 tidyr::separate,但即使那样也需要了解正则表达式,我想学习如何使用 () 和反向引用。)

这可能有帮助:

library(tools)
as.data.frame(cbind(dirname(files_list), file_path_sans_ext(basename(files_list)), file_ext(files_list)))
#                                            V1              V2  V3
#1 C:/User/Name/Folder/Subfolder1/Sub-subfolder            file txt
#2 C:/User/Name/Folder/Subfolder1/Sub-subfolder     file - Copy txt
#3 C:/User/Name/Folder/Subfolder1/Sub-subfolder        file (1) txt
#4 C:/User/Name/Folder/Subfolder1/Sub-subfolder file - Copy (2) txt
#5               C:/User/Name/Folder/Subfolder1           fileB txt
#6                          C:/User/Name/Folder          file.C txt
#7                          C:/User/Name/Folder          file-D txt
#8                          C:/User/Name/Folder            file    

我仍然不知道你是否需要它们作为字符串:如下所示

gsub("[/().]| - "," ",sub(".*?([^/]+/[^/]+$)","\1",files_list))

[1] "Sub-subfolder file txt"         
[2] "Sub-subfolder file Copy txt"    
[3] "Sub-subfolder file  1  txt"     
[4] "Sub-subfolder file Copy  2  txt"
[5] "Subfolder1 fileB txt"           
[6] "Folder file C txt"              
[7] "Folder file-D txt"              
[8] "Folder file"  

如果您只需要一种模式,那么:

pattern="[^/]+(?=/[^/]+$)|\w+(?=[ ).-])|\w+$"
regmatches(files_list,gregexpr(pattern,files_list,perl = TRUE))

Demo

抱歉,如果这不是最好的方法。我发现我的问题不完整,我想让问题更完整,同时也分享我想出的解决方案。

我希望这段代码能够处理所有可能的名称结构:

  1. "C:/" 或任何其他 directory/subdirectory
  2. 中的文件
  3. 文件名具有以下任何一项characters/features
    • “。”之前 ”。”在文件扩展名的开头
    • “-”或“-”不是“-复制”的一部分
    • “”或“(”不是文件名末尾的“(number)”的一部分

我使用此代码生成示例文件 names/paths 涵盖所有 folder/name/Copy/number/extension 组合:

files.df <- expand.grid(
    c("C:/"), 
    c("", "F1/", "F1/F2/"), 
    c("folder/"), 
    c("file"), 
    c("", " space", "-dash", " - spacedash", ".period", ".firstperiod.secondperiod"), 
    c("", 1, " 1", 10, " 10"), 
    c("", " - Copy"), 
    c("", " (1)", " (10)"), 
    c("", ".999", ".aaa"), 
    stringsAsFactors = F
)

for (i in 1:nrow(files.df)) {
    if (!exists("x")) {
        x <- vector(mode="character", length=0)
    }
    x[i] <- paste(as.character(as.vector(files.df[i, ])), sep = "", collapse = "")
}

通过使用(regex101,感谢@Onyambu!)的大量试验和错误,我整理了以下实际有效的可笑正则表达式:

sum(grepl(
    "^.:/(([^/]+)(?=/)/?)*(?<=/)(([^/](?! - Copy| \([0-9]+\)|\.[^/\.]+$))+.)( - )?((?<= - )Copy(?= \([0-9]+\)(?=\.[^/\.]+$|$)|\.[^/\.]+$|$))?( \()?((?<= \()([0-9]+)\)(?=\.[^/\.]+$|$))?\.?((?<=\.)([^/\.]+))?$", 
    x,
    perl = T
))
[1] 1620

length(x)
[1] 1620

不幸的是,这个正则表达式包含 10 个捕获组,我只能反向引用其中的 9 个(#10 是文件扩展名)。所以我将使用@RHertel 的更优雅的解决方案。但是,如果有人看到减少捕获组数量的方法,请告诉我!