[R] Regular expression to find value between brackets

2010-10-13 Thread Bart Joosen
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

Re: [R] Regular expression to find value between brackets

2010-10-13 Thread Henrique Dallazuanna
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

Re: [R] Regular expression to find value between brackets

2010-10-13 Thread Erik Iverson
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

Re: [R] Regular expression to find value between brackets

2010-10-13 Thread Bert Gunter
One way: gsub(.*\\(([^()]*)\\).*, \\1,tests) Idea: Pick out the units designation between the () and replace the whole expression with it. The \\1 refers to the [^()]* parenthesized expression in the middle that picks out the units. Cheers, Bert On Wed, Oct 13, 2010 at 11:16 AM, Bart Joosen

Re: [R] Regular expression to find value between brackets

2010-10-13 Thread Bert Gunter
Note: My original proposal, not quite right, can be made quite right via: gsub(.*\\((.*)\\).*||[^()]+, \\1,tests) The || or clause at the end handles the case where there are no parentheses in the string. -- Bert On Wed, Oct 13, 2010 at 11:16 AM, Bart Joosen bartjoo...@hotmail.com wrote:

Re: [R] Regular expression to find value between brackets

2010-10-13 Thread Matt Shotwell
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:

Re: [R] Regular expression to find value between brackets

2010-10-13 Thread Gabor Grothendieck
On Wed, Oct 13, 2010 at 2: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)) strapply in