[R] perl expression question

Gabor Grothendieck ggrothendieck at gmail.com
Tue Aug 12 23:12:21 CEST 2008


It can be done like this with strapply in gsubfn. See home
page at http://gsubfn.googlecode.com .  strapply is like
apply except the string to be operated on replaces the
matrix and the regexp replaces the margin number
and the function can be omitted if you are only returning
one item.    Its like strsplit in that it returns a list of one
component per input string but here we only have one
string so use [[1]].

fileName <- "Agg.20.20.20-all-01"

library(gsubfn)

# 20.20.20
strapply(fileName, "\\d\\d[.]\\d\\d[.]\\d\\d", perl = TRUE)[[1]]

# alternative - backref=-1 means only return portion in 1st (...)
strapply(fileName, "[.](..[.]..[.]..)-", backref = -1)[[1]]

# all
strapply(fileName, "-([^-]*)-", backref = -1)[[1]]

# both at once - backref=-2 means only return portion in 2 (...)'s
# c says to concatenate them into a character vector
strapply(fileName, "[.](..[.]..[.]..)-([^-]*)-", c, backref = -2)[[1]]


On Tue, Aug 12, 2008 at 4:18 PM,  <markleeds at verizon.net> wrote:
> I have a string such as
>
> fileName<-"Agg.20.20.20-all-01".
>
> All I want to do is pull the "20.20.20" and the "all" as strings.
>
> Obviously, they aren't always those values.
>
> The "20.20.20" can be "30.30.30" but it's always after the . which is next
> to the second g in Agg and it's always the same length. The all might not
> always be "all" but in general  it is the text between the two dashes.
>
> I made an attempt using below but perl expressions and myself are an
> extremely bad match ( pun was intended ) and all I got back was the same
> string as
> fileName in both cases.
>
>  weights=sub("^Agg.(\\.*)-\\.*$", "\1",fileName)
>  dashesstring=sub("^Agg.\\.*-(\\.*)-\\.*$", "\1",fileName)
>
> I could possibly using strsplit but I'm trying to learn perl expressions as
> best I can so a solution using that approach is really appreciated. Thanks.
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list