Slightly modified, also seems to work. gsubfn( "([[:alpha:]][[:alnum:]]*)((?=\\s*[-+~*)])|$)",function(x,...) paste0(toupper(x),'z'), test, perl=TRUE ) #[1] "Y1z + Y2z ~ Az*(Bz + Cz) + Dz + Fz * (h == 3) + (sex == 'male')*Iz" A.K.
----- Original Message ----- From: Greg Snow <538...@gmail.com> To: Frank Harrell <f.harr...@vanderbilt.edu> Cc: RHELP <r-h...@stat.math.ethz.ch> Sent: Thursday, August 15, 2013 5:07 PM Subject: Re: [R] regex challenge Here is a first stab: library(gsubfn) test <- "y1 + y2 ~ a*(b + c) + d + f * (h == 3) + (sex == 'male')*i" gsubfn( "([a-zA-Z][a-zA-Z0-9]*)((?=\\s*[-+~)*])|\\s*$)", function(x,...) paste0(toupper(x),'z'), test, perl=TRUE ) On Wed, Aug 14, 2013 at 9:13 PM, Frank Harrell <f.harr...@vanderbilt.edu>wrote: > I would like to be able to use gsub or gsubfn to process a formula and to > translate the variables but to ignore expressions in the formula. Supposing > that the R formula has already been transformed into a character string and > that the transformation is to convert variable names to upper case and to > append z to the names, an example would be to convert y1 + y2 ~ a*(b + c) + > d + f * (h == 3) + (sex == 'male')*i to Y1z + Y2z ~ Az*(Bz + Cz) + Dz + Fz > * (h == 3) + (sex == 'male')*Iz. Any expression that is not just a simple > variable name would be left alone. > > Does anyone want to try their hand at creating a regex that would > accomplish this? > > Thanks > Frank > -- > Frank E Harrell Jr Professor and Chairman School of Medicine > Department of Biostatistics Vanderbilt University > > ______________________________**________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide http://www.R-project.org/** > posting-guide.html <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com [[alternative HTML version deleted]] ______________________________________________ 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.