Re: [R] Perl-style regexes and capturing results

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Thu 04 May 2006 - 23:56:26 EST

gsub and sub support backreferences

sub("(.):(.)", "\\2:\\1", c("1:2", "5:6")) # c("2:1", "6:5")

# and you can capture them separately using strapply in the gsubfn package # or perform function-based substitutions using gsubfn.

# Returns: list(c("1", "2"), c("5", "6"))

library(gsubfn)
strapply(c("1:2", "5:6"), "(.):(.)", function(z,x,y) c(x,y))

# same
strapply(c("1:2", "5:6"), "(.):(.)", c, backref = -2)

On 5/4/06, Sean Davis <sdavis2@mail.nih.gov> wrote:
> Just a quick question....
>
> In perl, I can capture parts of a regex like /(\w)\/(\d)/ to get back $1 and
> $2 containing the parts of the regex in parentheses. Is there a parallel in
> R?
>
> Thanks,
> Sean
>
> ______________________________________________
> R-help@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
>



R-help@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 Received on Thu May 04 23:59:18 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 05 May 2006 - 02:09:59 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.