在gsub中使用Regex提取字符串

时间:2022-03-21 14:58:10

I try to use regex to extract string from R source code. I have this string :

我尝试使用正则表达式从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\")"
)

I would like to obtain :

我想获得:

 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")

I tried a lot of thing with gsub, but not sure what I do. can you please help me ?

我用gsub尝试了很多东西,但不知道我做了什么。你能帮我么 ?

Regards

问候

1 个解决方案

#1


2  

You could try

你可以试试

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"  

Update

Based on the new 'x'

基于新的'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"    

Update2

With the changes in 'x'

随着'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"                                                

#1


2  

You could try

你可以试试

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"  

Update

Based on the new 'x'

基于新的'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"    

Update2

With the changes in 'x'

随着'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"