Extract info inside all parenthesis in R

Here is an example:

> gsub("[\\(\\)]", "", regmatches(j, gregexpr("\\(.*?\\)", j))[[1]])
[1] "wonder" "groan"  "Laugh" 

I think this should work well:

> regmatches(j, gregexpr("(?=\\().*?(?<=\\))", j, perl=T))[[1]]
[1] "(wonder)" "(groan)"  "(Laugh)" 

but the results includes parenthesis… why?

This works:

regmatches(j, gregexpr("(?<=\\().*?(?=\\))", j, perl=T))[[1]]

Thanks @MartinMorgan for the comment.

Leave a Comment