用gsub从字符串中删除模式 [英] Remove pattern from string with gsub

查看:67
本文介绍了用gsub从字符串中删除模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力删除字符串中下划线之前的子字符串. 我想使用*(通配符)作为下划线可能有所不同的位:

I am struggling to remove the substring before the underscore in my string. I want to use * (wildcard) as the bit before the underscore can vary:

a <- c("foo_5", "bar_7")

a <- gsub("*_", "", a, perl = TRUE)

结果应如下所示:

> a
[1] 5 7

我也尝试了诸如"^ * "或?"之类的东西,但并没有真正起作用.

I also tried stuff like "^*" or "?" but did not really work.

推荐答案

以下代码适用于您的示例:

The following code works on your example :

gsub(".*_", "", a)

这篇关于用gsub从字符串中删除模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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