R语言中gsub使用示例记录
1、测试1
> bed <- c("NM_001378891.1_cds_0_0_chr6_117609655_r","NM_001378891.1_cds_1_0_chr6_117622137_r") > bed [1] "NM_001378891.1_cds_0_0_chr6_117609655_r" "NM_001378891.1_cds_1_0_chr6_117622137_r" > NM=gsub("(NM_.*?)_.*","\\1",bed) ## 括号内匹配的内容会被存储在"\\1"中, 为什么需要? > NM [1] "NM_001378891.1" "NM_001378891.1"
2、测试2
> bed <- c("NM_001378891.1_cds_0_0_chr6_117609655_r", "NM_001378891.1_cds_1_0_chr6_117622137_r") > test <- gsub("NM_.*?(_.*$)","\\1",bed) ## 括号中匹配的内容会被保存在"\\1"中 > test [1] "_cds_0_0_chr6_117609655_r" "_cds_1_0_chr6_117622137_r"
参考来源:https://mp.weixin.qq.com/s?__biz=MzI4ODE0NTE3OA==&mid=2649219537&idx=1&sn=6974c6e3b91b6e91ac407563f10d3835&chksm=f3d1aba4c4a622b243e58434d561e2a08db148fb98499eaec626f6e186ec5cfa912c6db5ab93&mpshare=1&scene=23&srcid=1118L4C0wtH1cYHmjtZD2lrk&sharer_sharetime=1637166249484&sharer_shareid=4ed060cc4cd1efce40e3ab6dd8d8c7d4#rd