【问题标题】:Find and replace text between two strings in R在R中的两个字符串之间查找和替换文本
【发布时间】:2021-10-14 05:49:30
【问题描述】:

我在一些 Rscripts 中创建了一些关于 R 的教程。我需要一个没有答案的讲义集(HS)和一个编码集(CS),学生可以在其中编码。我需要一些帮助正则表达式来搜索 HO 中的答案部分,以便将其从 CS 中删除。

在 HS 中,我在答案之前/之后有开始 (#'YOUR_ANSWER) 和结束 (#'END_ANSWER) 标志。要创建 HO 集,我需要替换

YOUR_ANSWER
As_samp2 = 36
As_samp3 = 38      
#'END_ANSWER

"space for answer".  

所以如果我的文字在:

a = "#'YOUR_ANSWER
       As_samp2 = 36
       As_samp3 = 38

       #'END_ANSWER"

我试过正则表达式,但没有替代品

b <-gsub(pattern = "YOUR_ANSWER(.*\n*)*#'END_ANSWER", a, replace="space for answer" )

如果我不使用正则表达式,即只需找到“YOUR_ANSWER” - 替换即有效

c <-gsub(pattern = "YOUR_ANSWER", a, replace="space for answer" )

如果我只是做正则表达式,正如预期的那样,所有文本都会被替换,即

d <- gsub(pattern = "(.*\n*)*", a, replace="space for answer" )

但组合不起作用。 正则表达式应该可以工作,请参阅:

https://regex101.com/r/USvzLF/1

所以一定有一些我没有得到的深层 R 魔法

    b <- gsub(pattern = "YOUR_ANSWER(.*\n*)*END_ANSWER", a, replace="space for answer" )
    c <- gsub(pattern = "YOUR_ANSWER", a, replace="space for answer" )
    d <- gsub(pattern = "(.*\n*)*", a, replace="space for answer" )

我希望将 YOUR_ANSWER 和 END_ANSWER 之间的所有内容替换为答案空间 但什么也没有发生。有任何想法吗? 现在更新@r2evans 向我展示了工作正则表达式; 我要更改的 R 脚本是 https://pastebin.com/mnjpkUFk(即 myfile) 我用来尝试更改它的代码(在单独的 R 脚本中)是: FileM

【问题讨论】:

  • 我很难理解您当前的文本是什么样的以及您希望将其转换成什么。请使用“之前”和“之后”转换的简化示例更新您的问题。
  • 这是你想要的吗? regex101.com/r/FfunIi/1
  • 不确定,但gsub("#'YOUR_ANSWER.*END_ANSWER", "(space for answer)", a) 工作得不够好?这实际上是您的 b ... 这也适用于我。
  • @MonkeyZeus 感谢您的帮助,但是 R 中的转义字符存在问题;错误消息是:错误:'\s' is an unrecognized escape in character string started ""YOUR_ANSWER\s". 所以在 Regex 101 中工作的东西在 R 中不起作用
  • 要保留#'YOUR_ANSWER#'END_ANSWER 并替换它们之间的内容吗?

标签: r regex string-substitution


【解决方案1】:

问题在于您将文件作为字符向量列表读入,并应用了一个期望单多行文本作为输入的正则表达式。

> FileM
 [1] "#'Rstudio environment"                                                             "#'==="                                                                            
 [3] " "                                                                                 "#'Top Left - scripts"                                                             
 [5] "#+"                                                                                "myfirstvariable = \"Hello R\"  #press control enter with cursor on line  "        
 [7] "myfirstvariable"                                                                   "As_samp1 = 34"                                                                    
 [9] " "                                                                                 "#'practical: create variables for arsenic concentration in 2 more samples"        
[11] "#+"                                                                                "#'YOUR_ANSWER"                                                                    
[13] "As_samp2 = 36"                                                                     "As_samp3 = 38"                                                                    
[15] " "                                                                                 "#'END_ANSWER"                                                                     
[17] "#+"                                                                                "#'Bottom Left - console"                                                          
[19] "#+"                                                                                "2+2"                                                                              
[21] " "                                                                                 "#'practical: calculate average As concentration, store result in variable As_mean"
[23] "#+"                                                                                "#'YOUR_ANSWER"                                                                    
[25] "As_mean<- (As_samp1 + As_samp2 + As_samp3)/3"                                      "#'END_ANSWER"                                                                     
[27] "#+"                                                                                "#'A word on comments"                                                             
[29] "#This is a comment"                                                                "#ignore #' and #+ <br/><br/>"     

因此,您应该在运行正则表达式之前加入这些行:

FileM <- paste(FileM, collapse="\n")

然后,使用

FileMedit <- gsub("YOUR_ANSWER.*?END_ANSWER", "space for answer", FileM)

现在,cat(FileMedit, collapse="\n") 显示

#'Rstudio environment
#'===
 
#'Top Left - scripts
#+
myfirstvariable = "Hello R"  #press control enter with cursor on line  
myfirstvariable
As_samp1 = 34
 
#'practical: create variables for arsenic concentration in 2 more samples
#+
#'space for answer
#+
#'Bottom Left - console
#+
2+2
 
#'practical: calculate average As concentration, store result in variable As_mean
#+
#'space for answer
#+
#'A word on comments
#This is a comment
#ignore #' and #+ <br/><br/>

现在,保存它:

cat(FileMedit, file = "outputfileM.R")

【讨论】:

    【解决方案2】:

    要获得更具体的匹配,您可以匹配第一行。然后匹配以下所有行,不以可选的前导水平空白字符开头,并将#'END_ANSWER 作为该行中唯一的文本。

    然后匹配最后一行,将匹配替换为space for answer

    #'YOUR_ANSWER.*(?:\R(?!\h*#'END_ANSWER$).*)*\R\h*#'END_ANSWER$
    

    Regex demo | R demo

    例如

    b <-gsub(pattern = "^#'YOUR_ANSWER.*(?:\\R(?!\\h*#'END_ANSWER$).*)*\\R\\h*#'END_ANSWER$", a, replace="space for answer", per=T)
    

    如果您想替换 YOUR_ANSWER 和 END_ANSWER 之间的内容,您可以使用 2 个捕获组并在替换中使用它们。

    ^(#'YOUR_ANSWER.*)(?:\R(?!\h*#'END_ANSWER$).*)*(\R\h*#'END_ANSWER)$
    

    Regex demo | R demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 2021-07-23
      • 2019-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多