[R] Regex: workaround for variable length negative lookbehind

Stefan Evert stefan.evert at uos.de
Sun Nov 30 21:59:13 CET 2008

>> But is there a one-line grep thingy to do this?
> Can't think of a one-liner, but a three-line solution you can easily  
> enough wrap in a small function:
> vec<-c("aaaa", "baaa", "bbaa", "bbba", "baamm", "aa")
> idx.1 <- grep("(.)\\1$", vec)
> idx.2 <- grep("^(.)\\1*$", vec)
> vec[setdiff(idx.1, idx.2)]

Oops, my bad, that solution was in Stefan's original mail already.  I  
got his example mixed up with some other text and thought he was  
talking about something different.

Still, I think it's better to write a few lines of R code than to  
abuse regular expressions to do something they were never intended to  
do.  How do other people on this list feel about that issue?

Sorry again, and next time I'll trink a cup of coffee _before_ I post!

More information about the R-help mailing list