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.
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"
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"
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"