[R] Help with grep (and similar functions)

Alberto Monteiro albmont at centroin.com.br
Wed Mar 28 15:20:04 CEST 2007


Gabor Grothendieck wrote:
>
> Try this:
> 
> library(gsubfn)
> pat <- "([[:upper:]][[:lower:]]*) "
> s <- "Aaaa 3 x 0 Bbbb"
> 
> strapply(s, pat, backref =-1)[[1]]
> 
Ok, I think I got it.

For example, if I want to retrieve Aaaa and Bbbb (which
could be any strings) or the numbers 3 and 0, I would do
this:

library(gsubfn)
pat <- "^([a-zA-Z ]+) ([1-9]*[0-9]) x ([1-9]*[0-9]) ([a-zA-Z ]+)$"
s <- "My team 3 x 0 the team from Outer Space"
x <- strapply(s, pat, c)[[1]]

Then x[1] is s, x[2] is "My team", x[3] is "3", x[4] is "0"
and x[5] is "the team from Outer Space".

Thanks.

Alberto Monteiro



More information about the R-help mailing list