Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-boun...@r-project.org 
> [mailto:r-help-boun...@r-project.org] On Behalf Of Dimitri Shvorob
> Sent: Thursday, August 26, 2010 3:16 AM
> To: r-help@r-project.org
> Subject: [R] Quick GREP challenge
> 
> 
> > grep("f[0-9]+=", "f1=5,f22=3,", value = T)
> [1] "f1=5,f22=3,"
> 
> How do I make the line output c("f1", "f22") instead? 
> (Actually, c(1,22)
> would be even better).

If you had S+ you could use subpattern and keep arguments
to strsplit (which otherwise acts like R's strsplit):

  > strings <- c("f1=5,f22=3,", "g4,f55,f66", "hello")
  > strsplit(strings, "f([[:digit:]]+)", subpattern=1, keep=TRUE)
  [[1]]:
  [1] "1"  "22"
  
  [[2]]:
  [1] "55" "66"

  [[3]]:
  character(0)

keep=TRUE means to split the string by what is not in
the pattern (keeping what is in the pattern) instead
of the usual splitting by what is in the pattern and
keeping what is not in the pattern.

subpattern=n means to match by the whole pattern but
to use only the n'th parenthesized subpattern when
deciding what to split by or keep.  In this case the
whole pattern matches 'f' followed by 1 or more digits
but we only want the keep the digits.  (subpattern=0,
the default, means to use the entire pattern.)

You still need to use as.integer() or as.numeric() on
the output elements.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 
> 
> 
> Thank you.
> 
>  
> -- 
> View this message in context: 
> http://r.789695.n4.nabble.com/Quick-GREP-challenge-tp2339486p2
339486.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help@r-project.org 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.
> 

______________________________________________
R-help@r-project.org 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.

Reply via email to