gsub跨R中的多行(dotall)

gsub across multiple lines in R (dotall)

是否可以在 R 中使用 gsub 调用 dotall 表达式?基本上,我试图提取一段跨越多行的文本。考虑以下示例:

eg.df <- c("----------", " ", "keep", " ", "keep this too", " ", "----------", " ", 
   "Delete this line and everything after", "Delete this one too", 
   " ", "And delete this one")

我想使用第 7-9 行作为我的模式来匹配。我想删除这些行以及后面的所有内容,直到文件末尾。

[1] "----------"                           
[2] " "                                    
[3] "keep"                                 
[4] " "                                    
[5] "keep this too"                        
[6] " "                                    
[7] "----------"                           
[8] " "                                    
[9] "Delete this line and everything after"
[10] "Delete this one too"                  
[11] " "                                    
[12] "And delete this one"

因此,结果输出将是:

[1] "----------"                           
[2] " "                                    
[3] "keep"                                 
[4] " "                                    
[5] "keep this too"                        
[6] " "               

你可以试试

  strsplit(sub('-+, +,[A-Za-z]+[^-]+$', '', 
         paste(eg.df, collapse= ',')), ',')[[1]]
  #[1] "----------"    " "             "keep"          " "            
  #[5] "keep this too" " "

或者正如@hwnd 评论的那样,

  strsplit(sub('-+[^-]+\z', '', paste(eg.df, collapse = '_'), 
                      perl=T), '_')[[1]]