首页 > 解决方案 > Remove numbers except that are before the first underscores in R with gsub

问题描述

I have this list:

l <- c("F1_6346346346_TrainTest_53453465.rds", "F1_64575687357_FunctionTest_747434534.rds", "F3F4_546345647678_TrainTest_453463654.rds"

I would like to have somethins like this:

l <- c("F1_TrainTest", "F1_FunctionTest", "F3F4_TrainTest")

I have tried with 'gsub', but I am dummy with regular expressions and I haven`t been able to achieve this.

Thank you in advance!

标签: regexgsub

解决方案


You can use this regex

_+\d+_*
  • _ - Matches _ one or more time.
  • \d+ - Matches digits 0 to 9 one or more time.
  • -* - Matches _ zero or more time.

DEMO


推荐阅读