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,
> 
> Alfredo


The 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

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

Reply via email to