Gonçalo Ferraz
2011-Apr-25 10:17 UTC
[R] regular expression for nth character in a string
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? Thank you for any help. Gon?alo
On 04/25/2011 08:17 PM, Gon?alo Ferraz wrote:> 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? >Hi Gon?alo, You could always try: Im<-"InTrouble" paste(unlist(strsplit(Im,""))[c(7,4,1,2,9,8,5,6,3)],collapse="") Jim
will this do it:> x <- "InTrouble" > sub("^(..).*", "\\1", x) # first two[1] "In"> sub(".*(...)$", "\\1", x) # last three[1] "ble"> sub("^..(...).*", "\\1", x) # 3rd,4th,5th char[1] "Tro">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? > > Thank you for any help. > > Gon?alo > > ______________________________________________ > 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
David Winsemius
2011-Apr-25 10:47 UTC
[R] regular expression for nth character in a string
On Apr 25, 2011, at 6:17 AM, Gon?alo Ferraz wrote:> 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?Not greppish but seems to be the obvious approach: > substr("Trouble", 1,2) [1] "Tr" > substr("Trouble", 3,5) [1] "oub" The greppish ways: > sub("(^..)(.*$)", "\\1", "Troubles") [1] "Tr" > sub("(^..)(...)(.*$)", "\\2", "Troubles") [1] "oub" > sub("(^..)(.{3})(.*$)", "\\2", "Troubles") [1] "oub" -- David Winsemius, MD West Hartford, CT
Gabor Grothendieck
2011-Apr-25 11:31 UTC
[R] regular expression for nth character in a string
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