[R] Regular expression to find value between brackets
Erik Iverson
eriki at ccbr.umn.edu
Wed Oct 13 20:41:39 CEST 2010
Bart,
I'm hardly one of the lists regex gurus: but this can
get you started...
tests <- c("pH", "Assay (%)", "Impurity A(%)", "content (mg/ml)")
x <- regexpr("\\((.*)\\)", tests)
substr(tests, x + 1, x + attr(x, "match.length") - 2)
Bart Joosen 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
More information about the R-help
mailing list