[R] gsub
Marc Schwartz
marc_schwartz at me.com
Fri Aug 6 20:50:57 CEST 2010
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
More information about the R-help
mailing list