[R] Regular expression to define contents between parentheses

Judith Flores juryef at yahoo.com
Tue Aug 25 22:17:48 CEST 2009


Hello dear R-helpers,

   I haven't been able to figure out of find a solution in the R-help archives about how to delete all the characters contained in groups of parenthesis. I have a vector that looks more or less like this:

myvector<-c("something (80 km/h, sd) & more (6 kg/L,sd)", "somethingelse (48 m/s, sd) & moretoo (50g/L , sd)")

I want to extract all the strings that are not contained in parenthesis, the goal would be to obtain the following new vector:

subvector<-c("something & more", "somethingelse & moretoo")

I tried the following, but this pattern seems to enclose all that is included between the first opened parenthesis and the last closed parethesis, which makes sense, but it's not what I need:

subvector<-gsub("\\((.*)\\)","",myvector


Your help will be very appreciated.

Thank you,

Judith




More information about the R-help mailing list