正则表达式 – 如何在R中只打10场比赛?

前端之家收集整理的这篇文章主要介绍了正则表达式 – 如何在R中只打10场比赛?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个数据文件,我可以grep“你”:

> dtq_ml_wuv[grep("you",dtq_ml_wuv$rn),"rn"]

"you can take"         "you can tell"         "you can thank"       
"you can try"          "you can turn"         "you can use"         
"you can visit"        "you can work"         "you donet know"      
"you donet need"       "you dont know"        "you get enough"      
"you get see"          "you go back"          "you got keep"        
"you guys can"         "you heard right"      "you just go"         
"you just gotta"       "you just look"        "you just need"       
"you just stay"        "you know better"      "you know else"       
"you know got"         "you know i"           "you know if"         
"you know im"          "you know it"          "you know just"       
"you know many"        "you know means"       "you know one"        
"you know really"      "you know right"       "you like see"

我怎么能让grep在发现之后停下来,比方说0到25场比赛?

我试过了

> dtq_ml_wuv[grep("you{0,25}","rn"]

但是由于{}的内容无效,它告诉我表达式无效.

任何提示都表示赞赏.

解决方法

这是一个分块版本.调整块大小以充分利用内部编译代码的速度,而不是搜索过多的代价.

grepn<-function(pattern,x,n,chunk.size=32) {
  N<-length(x);
  chunk<-1:chunk.size;
  k<-1;
  M<-vector("integer",n+chunk.size);
  while(k < n && chunk[1] <= N) {
    i<-na.omit(grep(pattern,x[chunk]));
    if(length(i)) M[k:(k+length(i)-1)]<-i+chunk[1]-1
    k<-k+length(i)
    chunk<-chunk+chunk.size
  }
  return(M[1:(min(k-1,n))])
}

将数据作为问题中的字符向量的示例

grepn("e",data,n=10,chunk.size=16)
[1]  1  2  6  9 10 12 13 15 17 21

猜你在找的正则表达式相关文章