Re: [R] regular expression for nth character in a string

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Mon, 25 Apr 2011 07:31:59 -0400

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

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Mon 25 Apr 2011 - 11:36:19 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 25 Apr 2011 - 12:40:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive