[R] regex question
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Aug 4 19:44:14 CEST 2009
And here is a second way:
> strapply(astr, "(\\w)\\w+", c, simplify = c)
[1] "T" "i" "m" "t" "o" "w" "t" "d" "i" "i" "t" "f" "f" "T"
On Tue, Aug 4, 2009 at 1:42 PM, Gabor
Grothendieck<ggrothendieck at gmail.com> wrote:
> Try this:
>
>> library(gsubfn)
>> strapply(astr, "\\w+", ~ substr(x, 1, 1), simplify = c)
> [1] "T" "i" "m" "t" "o" "w" "t" "d" "i" "i" "t" "f" "f" "T"
>
>
> On Tue, Aug 4, 2009 at 1:28 PM, ravi<rv15i at yahoo.se> wrote:
>>
>> Hi,
>> I am getting stuck over an apparently simple problem in the use of regular expressions :
>> To collect together the first letters of the words from the Perl motto, “There is more than one way to do it” in the following form – TIMTOWTDI.
>> I tried the following code :
>>
>> ##### A regex problem with the Perl motto
>> astr<-"There is more than one way to do it"
>> b1<-grep("\\<", astr,value=T)
>> ## This just retrieves the whole string
>> ## Next trial with gregexpr
>> b2<-gregexpr("\\<",astr)
>> ## This gives :
>>> b3
>> [[1]]
>> [1] 1 7 10 15 20 24 28 31 34
>> attr(,"match.length")
>> [1] 0 0 0 0 0 0 0 0 0
>>
>> A vector of indices corresponding to the first letter is obtained all right with gregexpr but the next step is not so clear. I am not able to figure out how I can use this information to pick out the letters from the original string. My problem is that I don’t know how I can treat the string as a vector and pluck out the letters.
>>
>> There may be many ways to do it, but I have not succeeded in coming up with even one way! I will appreciate any tips that I can get.
>> Thanking you,
>> Ravi
>>
>> ______________________________________________
>> 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.
>>
>
More information about the R-help
mailing list