如何在R中使用grep来获取指定的字符?

Ff *_* Yy 1 grep r

我有

str=c("00005.profit", "00005.profit-in","00006.profit","00006.profit-in")  
Run Code Online (Sandbox Code Playgroud)

而且我想得到

 "00005.profit"  "00006.profit"
Run Code Online (Sandbox Code Playgroud)

如何grep在R中实现此目的?

Dir*_*tel 8

这是一种方式:

R> s <- c("00005.profit", "00005.profit-in","00006.profit","00006.profit-in")
> unique(gsub("([0-9]+.profit).*", "\\1", s))
[1] "00005.profit" "00006.profit"
R> 
Run Code Online (Sandbox Code Playgroud)

我们将正则表达式定义为数字后跟.profit,我们通过将表达式保留在parantheses中来指定.在\\1随后回顾了第一个这样的任务-作为我们记得没有别的,这就是我们得到的.该unique()则减少了四个项目,以两个独特的.