[英]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.