R 中正则

在R中使用正则,可以指定perl=True 构造复杂正则,这也是本人比较熟悉在方式 

> word <- c('a1:23asdfjaskldjf<->aa:112xzvasoiffncx909<->bb:23asdfjaskldjfAAA')

> pattern<- 'bb.*(?=(<->|$))'
> (gregout <- gregexpr(pattern,word,perl=TRUE))
[[1]]
[1] 45
attr(,"match.length")
[1] 20
attr(,"useBytes")
[1] TRUE
attr(,"capture.start")
       
[1,] 65
attr(,"capture.length")
      
[1,] 0
attr(,"capture.names")
[1] ""

> substr(word[1],gregout[[1]],gregout[[1]]+attr(gregout[[1]],'match.length')-1)
[1] "bb:23asdfjaskldjfAAA"

 正则中使用了向前引用,使用了() ,可以使用perl=TRUE 来指定 

原文地址:https://www.cnblogs.com/yunfeiqi/p/6859936.html