1

I have a string:

t="abc,mno,pqr,xyz,qwe,asd"

i want all the possible 3 consecutive words as the output, like this:

"abc mno pqr,mno pqr xyz,pqr xyz qwe,xyz qwe asd"

I am using R, so perl regex engine is to be used.

Any ideas?

Thanks.

mpapec
  • 50,217
  • 8
  • 67
  • 127
sidpat
  • 735
  • 10
  • 26

3 Answers3

6

Ok I figured it out myself,

t="abc,mno,pqr,xyz,qwe,asd"
t=gsub("(?=,([^,]*),([^,]*))", " \\1 \\2", t, perl=T)
t=gsub("(,[^,]*,[^,]*)$", "", t, perl=T)
t
"abc mno pqr,mno pqr xyz,pqr xyz qwe,xyz qwe asd"
sidpat
  • 735
  • 10
  • 26
  • i think you got some fun in asking these type of questions http://stackoverflow.com/questions/25441766/string-transformation-in-r-grouping-words-of-a-string – Avinash Raj Nov 05 '14 at 02:08
3

Here's a non-regex possible solution

t <- "abc,mno,pqr,xyz,qwe,asd" 
library(zoo)
paste(rollapply(strsplit(t, ",")[[1]], width = 3, FUN = paste, collapse = " "), collapse = ",")
## [1] "abc mno pqr,mno pqr xyz,pqr xyz qwe,xyz qwe asd"
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
1

Another gsub method,

> t="abc,mno,pqr,xyz,qwe,asd"
> m <- gsub("(?=,([^,]+),([^,]+)\\b(?!$))", " \\1 \\2", t, perl=TRUE)
> result <- gsub(",(?=(?:[^,]*,)?[^,]*$)", " ", m, perl=TRUE)
> result
[1] "abc mno pqr,mno pqr xyz,pqr xyz qwe,xyz qwe asd"
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274