I have created some tutorials on R in some Rscripts. I need a Handout Set(HS) and a Coding Set (CS) without answers in which students can code . I need some help regex to search for the answer section in HO so I can remove it from the CS.
In the HS I have beginning (#'YOUR_ANSWER)and end (#'END_ANSWER) flags before/after the answers. To create the HO set I need to replace
YOUR_ANSWER
As_samp2 = 36
As_samp3 = 38
#'END_ANSWER
with
"space for answer".
So if my text is in a:
a = "#'YOUR_ANSWER
As_samp2 = 36
As_samp3 = 38
#'END_ANSWER"
I have tried regex but there is no replacement
b <-gsub(pattern = "YOUR_ANSWER(.*\n*)*#'END_ANSWER", a, replace="space for answer" )
if I dont use regex ie just find "YOUR_ANSWER" - the replacement works ie
c <-gsub(pattern = "YOUR_ANSWER", a, replace="space for answer" )
if I just do regex, as expected all text is substituted ie
d <- gsub(pattern = "(.*\n*)*", a, replace="space for answer" )
but the combination doesnt work. The regex should work see:
https://regex101.com/r/USvzLF/1
So there must be some deep R magic that I'm not getting
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" )
I expect to have replaced all between YOUR_ANSWER and END_ANSWER with space for answer But nothing happens. Any ideas? UPDATE now @r2evans has shown me working regex; The R script I am trying to change is https://pastebin.com/mnjpkUFk (ie myfile) And the code I am using to try and change it (in a separate R script) is: 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 = "outputfileM.R")