[R] backreferences in gregexpr

Gabor Grothendieck ggrothendieck at gmail.com
Fri Nov 2 23:14:01 CET 2012


On Fri, Nov 2, 2012 at 6:02 PM, Alexander Shenkin <ashenkin at ufl.edu> wrote:
> Hi Folks,
>
> I'm trying to extract just the backreferences from a regex.
>
>> temp = "abcd1234abcd1234"
>> regmatches(temp, gregexpr("(?:abcd)(1234)", temp))
> [[1]]
> [1] "abcd1234" "abcd1234"
>
> What I would like is:
> [1] "1234" "1234"
>
> Note: I know I can just match 1234 here, but the actual example is
> complicated enough that I have to match a larger string, and just want
> to pass out the backreferenced portion.
>
> Any help greatly appreciated!
>

Try this:

> library(gsubfn)
> strapplyc(temp, "abcd(1234)")
[[1]]
[1] "1234" "1234"

-- 
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