Re: [R] extracting text contained in brackets ("[ ... ]") from a character string?

From: jim holtman <jholtman_at_gmail.com>
Date: Sun, 13 May 2007 17:35:36 -0400

here is one way using 'sub':

> x <- c("testdata[3]", "testdata[-4]", "testdata[-4g]")
> sub(".*\\[(.*)\\].*", "\\1", x, perl=TRUE)
[1] "3" "-4" "-4g"
> x.func <- function(x){ sub(".*\\[(.*)\\].*", "\\1", x, perl=TRUE)}
> x.func(x)

[1] "3" "-4" "-4g"
>

On 5/13/07, new ruser <newruser_at_yahoo.com> wrote:
> I have a text string that contains text within two brackets.
>
> e.g. "testdata[3]" "testdata[-4]", "testdata[-4g]",
>
> I wish to "extract" the string enclosed in brackets?
>
> What is a good way to do this?
>
> e.g.
>
> fun(testdata[3]) = '3'
>
> fun(testdata[-4g]) = '-4g'
>
> ---------------------------------
> Moody friends. Drama queens. Your life? Nope! - their life, your story.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_stat.math.ethz.ch 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.
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
R-help_at_stat.math.ethz.ch 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.
Received on Sun 13 May 2007 - 21:45:38 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 14 May 2007 - 00:31:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.