[英]Find and replace text between two strings in R
我在一些 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 <- readLines(myfile) FileMedit <- gsub(pattern = "YOUR_ANSWER", FileM, replace="space for answer" ) FileMedit <- gsub(pattern = "YOUR_ANSWER.*END_ANSWER", FileM, replace="space for answer") writeLines(FileMedit,file = “输出文件M.R”)
问题是您将文件作为字符向量列表读取并应用期望单个多行文本作为输入的正则表达式。
> 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")
要获得更具体的匹配,您可以匹配第一行。 然后匹配所有以下行,不要以可选的前导水平空白字符#'END_ANSWER
, #'END_ANSWER
作为该行上的唯一文本。
然后匹配最后一行并将匹配替换为space for answer
#'YOUR_ANSWER.*(?:\R(?!\h*#'END_ANSWER$).*)*\R\h*#'END_ANSWER$
例如
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)$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.