Here's a shorter (but more cryptic) one: > gsub("^([^\\(]+)(\\((.+)\\))?", "\\2", tests) [1] "" "(%)" "(%)" "(mg/ml)"
> gsub("^([^\\(]+)(\\((.+)\\))?", "\\3", tests) [1] "" "%" "%" "mg/ml" -Matt On Wed, 2010-10-13 at 14:34 -0400, Henrique Dallazuanna wrote: > Try this: > > replace(gsub(".*\\((.*)\\)$", "\\1", tests), !grepl("\\(.*\\)", tests), "") > > > On Wed, Oct 13, 2010 at 3:16 PM, Bart Joosen <bartjoo...@hotmail.com> wrote: > > > > > Hi, > > > > this should be an easy one, but I can't figure it out. > > I have a vector of tests, with their units between brackets (if they have > > units). > > eg tests <- c("pH", "Assay (%)", "Impurity A(%)", "content (mg/ml)") > > > > Now I would like to hava a function where I use a test as input, and which > > returns the units > > like: > > f <- function (x) sub("\\)", "", sub("\\(", "",sub("[[:alnum:]]+","",x))) > > this should give "", "%", "%", "mg/ml", but it doesn't do the job quit > > well. > > > > After searching in the manual, and on the help lists, I cant find the > > answer. > > > > anyone? > > > > Bart > > -- > > View this message in context: > > http://r.789695.n4.nabble.com/Regular-expression-to-find-value-between-brackets-tp2994166p2994166.html > > Sent from the R help mailing list archive at Nabble.com. > > > > ______________________________________________ > > R-help@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > > -- Matthew S. Shotwell Graduate Student Division of Biostatistics and Epidemiology Medical University of South Carolina ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.