Hi, I'm using gsub, but I've a problem.> print(i)[1] "piante_venere.csv"> gsub("\\.csv$", "", i)[1] "piante_venere"> gsub("^piante_", "", i)[1] "venere.csv" Can I combine the two expressions? Like this:> gsub(.....)[1] "venere" Thanks, Alfredo -- View this message in context: http://r.789695.n4.nabble.com/gsub-tp2316443p2316443.html Sent from the R help mailing list archive at Nabble.com.
i <- "piante_venere.csv" gsub("^.*_(.*)\\.csv$", "\\1", i) ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/gsub-tp2316443p2316550.html Sent from the R help mailing list archive at Nabble.com.
On Aug 6, 2010, at 10:14 AM, Alfredo Alessandrini wrote:> > Hi, > > I'm using gsub, but I've a problem. > >> print(i) > [1] "piante_venere.csv" >> gsub("\\.csv$", "", i) > [1] "piante_venere" >> gsub("^piante_", "", i) > [1] "venere.csv" > > > Can I combine the two expressions? > > Like this: > >> gsub(.....) > [1] "venere" > > Thanks, > > AlfredoThe easiest way is to use a back reference to return the part of the vector that you want:> gsub("^.*_(.*)\\.csv$", "\\1", "piante_venere.csv")[1] "venere" In this case, the "\\1" returns the part of the regex defined within the parens. HTH, Marc Schwartz