我有一个字符串和什么来提取多个括号内的信息。目前我可以用下面的代码从最后一个括号中提取信息。我该如何做,所以它提取多个括号和返回作为一个向量?
j <- "What kind of cheese isn't your cheese? (wonder) Nacho cheese! (groan) (Laugh)" sub("\\).*","",sub(".*\\(",j))@H_403_3@电流输出为:
[1] "Laugh"@H_403_3@所需输出为:
[1] "wonder" "groan" "Laugh"
这是一个例子:
> gsub("[\\(\\)]",regmatches(j,gregexpr("\\(.*?\\)",j))[[1]]) [1] "wonder" "groan" "Laugh"@H_403_3@我认为这应该很好:
> regmatches(j,gregexpr("(?=\\().*?(?<=\\))",j,perl=T))[[1]] [1] "(wonder)" "(groan)" "(Laugh)"@H_403_3@但结果包括括号…为什么? @H_403_3@这样做:
regmatches(j,gregexpr("(?<=\\().*?(?=\\))",perl=T))[[1]]@H_403_3@感谢@MartinMorgan的评论。