使用 gsub 删除 R 中第一个下划线之前的数字
Remove numbers except that are before the first underscores in R with gsub
我有这个列表:
l <- c("F1_6346346346_TrainTest_53453465.rds", "F1_64575687357_FunctionTest_747434534.rds", "F3F4_546345647678_TrainTest_453463654.rds"
我想要这样的东西:
l <- c("F1_TrainTest", "F1_FunctionTest", "F3F4_TrainTest")
我已经尝试过 'gsub',但我对正则表达式一窍不通,我没能做到这一点。
提前致谢!
你可以使用这个正则表达式
_+\d+_*
_
- 匹配 _
一次或多次。
\d+
- 匹配数字 0 to 9
一次或多次。
-*
- 匹配 _
零次或多次。
(_(\d)+(\.rds)*)
什么都不替换,得到:
l <- c("F1_TrainTest", "F1_FunctionTest", "F3F4_TrainTest"
获取可以选择后跟 .rds 的所有数字组
我有这个列表:
l <- c("F1_6346346346_TrainTest_53453465.rds", "F1_64575687357_FunctionTest_747434534.rds", "F3F4_546345647678_TrainTest_453463654.rds"
我想要这样的东西:
l <- c("F1_TrainTest", "F1_FunctionTest", "F3F4_TrainTest")
我已经尝试过 'gsub',但我对正则表达式一窍不通,我没能做到这一点。
提前致谢!
你可以使用这个正则表达式
_+\d+_*
_
- 匹配_
一次或多次。\d+
- 匹配数字0 to 9
一次或多次。-*
- 匹配_
零次或多次。
(_(\d)+(\.rds)*)
什么都不替换,得到:
l <- c("F1_TrainTest", "F1_FunctionTest", "F3F4_TrainTest"
获取可以选择后跟 .rds 的所有数字组