删除特定字符最后一次出现后的字符 [英] Remove characters after the last occurrence of a specific character

查看:38
本文介绍了删除特定字符最后一次出现后的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的字符串:

I have a string that looks like:

exampleList <- c("rs40535:1745233:G:A_AGGG","rs41111:1733320:GAC:AAC_TTTTTTG", "exm2344379:1724237:A:T_A", "exm-rs234380:1890910:A:G_A", "rs423444419_T","psy_rs73453432_TCCC","22:1701234072:C:T_C","9:4534345:rs2342342_G","chr10_rs7287862_C","psy_rs7291672_A")  

我希望删除最后一个下划线 (_) 之后的所有内容,因此我的结果如下所示:

I wish to remove everything after the last underscore ( _ ) so my result looks something like this:

[1] "rs40535:1745233:G:A"      "rs41111:1733320:GAC:AAC"  "exm2344379:1724237:A:T"   "exm-rs234380:1890910:A:G"   "rs423444419"              "psy_rs73453432"           "22:1701234072:C:T"        "9:4534345:rs2342342"     "chr10_rs7287862"          "psy_rs7291672"    

我尝试了以下方法,但这会删除第一个 _ 之后的所有内容.

I've tried the following, but this removes everything after the first _.

gsub("\\_.*$","",exampleList) 

我知道有类似的帖子,但我在 R 中找不到.

I recognize there are similar posts but none I could find in R.

推荐答案

想通了!

outcome <- sub("_[^_]+$", "", exampleList)

这篇关于删除特定字符最后一次出现后的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆