Gundala Viswanath
2013-Jul-10 07:02 UTC
[R] Replacing part of delimited string with R's regex
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 What's the way to do it? [[alternative HTML version deleted]]
David Winsemius
2013-Jul-10 08:05 UTC
[R] Replacing part of delimited string with R's regex
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
Hi You could use: ?gsub("([[:alnum:]]+-)([[:alnum:]]+-)(.*)","\\1\\2zzz",name) #[1] "hsa-miR-zzz" "hsa-miR-zzz" "hsa-let-zzz" A.K. ----- Original Message ----- From: Gundala Viswanath <gundalav at gmail.com> To: "r-help at stat.math.ethz.ch" <r-help at stat.math.ethz.ch> Cc: Sent: Wednesday, July 10, 2013 3:02 AM Subject: [R] Replacing part of delimited string with R's regex 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 What's the way to do it? ??? [[alternative HTML version deleted]] ______________________________________________ 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.