[R] Regexp subexpression

Dieter Menne dieter.menne at menne-biomed.de
Sat Mar 25 18:24:04 CET 2006


Gabor Grothendieck <ggrothendieck <at> gmail.com> writes:

> 
> In the third case there is no match so there are no
> substitutions.  Handle it separately:
> 
> pat = "^([[:alpha:]]+)([[:digit:]]+)"
> result <- cbind(txt = sub(pat, "\\1", patid), num = sub(pat, "\\2", patid))
> result[regexpr(pat, paid) < 0,] <- NA
> 

Thanks, Gabor, that something like a compressed version of mine.  My main 
question was if I was missing something obvious, because I found the double sub 
messy. I am a surprised that there is not 

pat = "^([[:alpha:]]+)([[:digit:]]+)"
mygrep(pat, patid)

returning a list with all subexpressions.

Dieter




More information about the R-help mailing list