[R] Regexp subexpression

Duncan Temple Lang duncan at wald.ucdavis.edu
Sat Mar 25 19:10:09 CET 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



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
them up.

> 
> Dieter
> 
> ______________________________________________
> 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

- --
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
Davis,
CA 95616,
USA
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFEJYeB9p/Jzwa2QP4RAnAEAJwP+3Gr6RJLje+m9oOSwTlsdoN72ACeKLyM
d3eoIYuZERKv2AzibwiMPM4=
=79U5
-----END PGP SIGNATURE-----




More information about the R-help mailing list