使用str_extract

时间:2019-09-23 17:20:48

标签: r stringr

我有一些看起来像这样的变量标签。

如何使用纵梁提取方括号之间的文本

var1<-' [I don\'t think public officials care much what people like me think.] 
For the following questions, please indicate whether you strongly agree, agree, 
disagree, or strongly disagree.'
var1
library(stringr)
str_extract(var1, '\[.\]')

1 个答案:

答案 0 :(得分:1)

一种选择是使用正则表达式环顾四周,以匹配不是在方括号之后的方括号的所有字符

library(stringr)
str_extract(var1, "(?<=\\[)[^]]+")
#[1] "I don't think public officials care much what people like me think."

数据

var1 <- " [I don't think public officials care much what people like me think.] \n\t\tFor the following questions, please indicate whether you strongly agree, agree, \n\t\tdisagree, or strongly disagree."