[R] Replacing part of delimited string with R's regex

David Winsemius dwinsemius at comcast.net
Wed Jul 10 10:05:37 CEST 2013


On Jul 10, 2013, at 12:02 AM, Gundala Viswanath wrote:

> I have the following list of strings:
> 
> name <- c("hsa-miR-555p","hsa-miR-519b-3p","hsa-let-7a")
> 
> What I want to do is for each of the above strings
> replace the text after second delimiter with "zzz".
> Yielding:
> 
> hsa-miR-zzz
> hsa-miR-zzz
> hsa-let-zzz

?regex

Look at sections on character classe, repetition quantifiers, and back references.

> sub("(^[^-]*-[^-]*-)(.*$)", "\\1zzz", name)
[1] "hsa-miR-zzz" "hsa-miR-zzz" "hsa-let-zzz"

-- 
David Winsemius
Alameda, CA, USA



More information about the R-help mailing list