[R] Extract some character from a character vector of length 1

Gabor Grothendieck ggrothendieck at gmail.com
Wed Nov 29 08:06:42 CET 2006


The following matches pat1 followed by an ungreedy match of
what is in between followed by pat2.  What is in between is
defined to be backreference 1 which is returned.  See:
   http://code.google.com/p/gsubfn/
for more on strapply and the gsubfn package.

> # test data
> a <- "something2 ....pat1 name1 pat2 something2....pat1 name2 pat2....pat1 name3 pat2  "
>
> library(gsubfn)
> strapply(a, "(?U)pat1 (.*) pat2", backref = -1, perl = TRUE)[[1]]
[1] "name1" "name2" "name3"


On 11/28/06, ronggui <ronggui.huang at gmail.com> wrote:
> the content of th character vector (of length 1)  is as follows:
>
> a <- "something2 ....pat1 name1 pat2 something2....pat1 name2
> pat2....pat1 name3 pat2  "
>
> I would like to extract the character bewteen pat1 and pat2. That's to
> say, I would like to get a vecter of c("name1", "name2","name3").
>
> What I did is use strsplit() twise. But I wonder if there any function
> to this specific task?
>
> Thanks.
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list