[R] Regexp subexpression
Duncan Temple Lang
duncan at wald.ucdavis.edu
Sat Mar 25 19:10:09 CET 2006
-----BEGIN PGP SIGNED MESSAGE-----
Dieter Menne wrote:
> 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.
I have been surprised about that also a long time back and
have code that I will get around to putting into R to allow
the matching subexpressions be returned as a character vector
to avoid having to do silly tricks with strsplit() to break
> R-help at stat.math.ethz.ch mailing list
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Duncan Temple Lang duncan at wald.ucdavis.edu
Department of Statistics work: (530) 752-4782
4210 Mathematical Sciences Building fax: (530) 752-7099
One Shields Ave.
University of California at Davis
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)
-----END PGP SIGNATURE-----
More information about the R-help