[R] strapply and characters adjacent to the matched pattern

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jul 26 13:08:56 CEST 2012


On Wed, Jul 25, 2012 at 4:34 PM, mdvaan <mathijsdevaan at gmail.com> wrote:
> Thanks Gabor. That worked really well. I have been reading about the use of
> POSIX and regular expressions and I tried to use your example to see if I
> could  ignore all matches in which the character preceding (rather than
> following) the match is one of [:alpha:]? So far, I have been unsuccessful.
> Could anyone help me out here or direct me to a source that explains the
> combined use of POSIX and regular expressions? Thanks!
>

We match the start of the string or a non-alpha followed by the
desired string (here its xx).  Because we want the second back
reference (the default is to return the first back rerference if the
function is omitted) we must specifically tell it that by using a
function which returns its second argument, e.g. function(x, y) y or
function(...) ..2 or using the equivalent formula notation just ~ ..2
:

strapply(c("cxx", "xxc", "xx", " xx"), "(^|[^[:alpha:]])(xx)", ~ ..2)


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list