Skip to content
Back to formatted view

Raw Message

Message-ID: <BANLkTi=Nd-tDgh-aEgm0dQYLD4mbs3U4RA@mail.gmail.com>
Date: 2011-04-25T11:31:59Z
From: Gabor Grothendieck
Subject: regular expression for nth character in a string
In-Reply-To: <4A9E57C7-DBFD-4189-82A9-093BD1DAA1B7@gmail.com>

2011/4/25 Gon?alo Ferraz <gferraz29 at gmail.com>:
> Hi, I have a string
>
> "InTrouble"
>
> and want to extract, say, the first two characters: "In"
> or the last three: "blee"
> or the 3rd, 4th, and 5th: "Trou"
>
> Is there an easy way of doing this quickly with regular expressions in gsub, grep or similar?
>

strapply in gsubfn can readily do that.  It returns the matched part
or, if parentheses are used, only the part in parentheses:

> library(gsubfn)
> strapply("InTrouble", "^..", simplify = TRUE)
[1] "In"
> strapply("InTrouble", "...$", simplify = TRUE)
[1] "ble"
> strapply("InTrouble", "^..(...)", simplify = TRUE)
[1] "Tro"
> strapply("InTrouble", "^.{2}(.{3})", simplify = TRUE)
[1] "Tro"


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