我尝试使用正则表达式从R源代码中提取字符串。 我有这个字符串:
x<-c(" stop(\"You forgot to specify the correct answer on a multiple choice question!\")",
"stop(\"this value\",var,\"is ok\")",
"stop(args=anything,message=\"hi how are you\")",
"PLOP(args=anything,message=\"DONT WANT THIS ONE\")",
" BIDUL(\"DONT WANT THIS ONE\")",
"stop(args=anything,message=\"THIS ONE IS OK\"); BIDUL(\"DONT WANT THIS ONE\")"
)
我想获得:
result<- c("You forgot to specify the correct answer on a multiple choice question!","this value","is ok","hi how are you","THIS ONE IS OK")
我用gsub尝试了很多东西,但不确定我做了什么。 你能帮帮我吗?
此致
答案 0 :(得分:2)
你可以尝试
library(stringr)
unlist(str_extract_all(x, perl('(?<=\")[^,\"]+(?=\")')))
#[1] "You forgot to specify the correct answer on a multiple choice question!"
#[2] "this value"
#[3] "is ok"
#[4] "hi how are you"
基于新的'x'
unlist(str_extract_all(x[grep('stop', x)], perl('(?<=\")[^,\"]+(?=\")')))
#[1] "You forgot to specify the correct answer on a multiple choice question!"
#[2] "this value"
#[3] "is ok"
#[4] "hi how are you"
随着'x'的变化
v1 <- str_extract(x[grep('stop', x)], perl('(?<=stop)[^)]+(?=\\))'))
unlist(str_extract_all(v1, perl('(?<=\")[^,\"]+(?=\")')))
#[1] "You forgot to specify the correct answer on a multiple choice question!"
#[2] "this value"
#[3] "is ok"
#[4] "hi how are you"
#[5] "THIS ONE IS OK"