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