Remove first 4 words after a certain string pattern in R?

Viewed 138

I am working with really long strings. How can I remove the first 4 words after a certain string pattern occurs? For example:

string <- "hello I am a user of stackoverflow and I am really happy with all the help the community usually offers when I'm in need of some coding expertise." 

#remove the fist 4 words after and including "stackoverflow" 

result
"hello I am a user of happy with all the help the community usually offers when I'm in need of some coding expertise." 

3 Answers

Solution with base R

A one line solution:

pattern <- "stackoverflow"
string <- "hello I am a user of stackoverflow and I am really happy with all the help the community usually offers when I'm in need of some coding expertise." 

gsub(paste0(pattern, "(\\W+\\w+){0,4}\\W*"), "", string)
#> [1] "hello I am a user of happy with all the help the community usually offers when I'm in need of some coding expertise."

How it works

Create the pattern you want with a regex: "stackoverflow" followed by 4 words. Definitely, check out ?regex for more info about it.

Words are identified by \\w+ and separators are identified by \\W+ (capital w, it includes spaces and special characters like the apostrophe that you have in the sentence)

(...){0,4} means that the combination of word and separator may repeat up to 4 times.

\\W* needs to identify a possible final separator, so that the remaining two pieces of the sentence won't have two separators dividing them. Try it without, you'll see what I mean.

gsub locates the pattern you want and replace it with "" (thus deliting it).


Handle Exceptions

Note that it works even for particular cases:

# end of a sentence with fewer than 4 words after
string <- "hello I am a user of stackoverflow and I am" 
gsub(paste0(pattern, "(\\W+\\w+){0,4}\\W*"), "", string)
#> [1] "hello I am a user of "

# beginning of a sentence
string <- "stackoverflow and I am really happy with all the help the community usually offers when I'm in need of some coding expertise." 
gsub(paste0(pattern, "(\\W+\\w+){0,4}\\W*"), "", string)
#> [1] "happy with all the help the community usually offers when I'm in need of some coding expertise."

# pattern == string
string <- "stackoverflow and I am really" 
gsub(paste0(pattern, "(\\W+\\w+){0,4}\\W*"), "", string)
#> [1] ""

A tidyverse solution

library(stringr)

# locate start and end position of pattern
tmp <- str_locate(string, paste0(pattern,"(\\W+\\w+){0,4}\\W*"))

# get positions: start_sentence-start_pattern and end_pattern-end_sentence
tmp <- invert_match(tmp)

# get the substrings
tmp <- str_sub(string, tmp[,1], tmp[,2])

# collapse substrings together
str_c(tmp, collapse = "")
#> [1] "hello I am a user of happy with all the help the community usually offers when I'm in need of some coding expertise."

Search for your pattern with additional spaces and words after it. Find the positions of the first last match, split the string and paste it back together. At the end gsub any double (or more) spaces.

string <- "hello I am a user of stackoverflow and I am really happy with all the help the community usually offers when I'm in need of some coding expertise." 

pat="stackoverflow"

library(stringr)
tmp=str_locate(
  string,
  paste0(
    pat,
    paste0(
      rep("\\s?[a-zA-Z]+",4),
      collapse=""
    )
  )
)

gsub("\\s{2,}"," ",
  paste0(
    substring(string,1,tmp[1]-1),
    substring(string,tmp[2]+1)
  )
)

[1] "hello I am a user of happy with all the help the community usually offers when I'm in need of some coding expertise."

Quick answer, I am sure you can have better code thant that:

string <- "hello I am a user of stackoverflow and I am really happy with all the help the community usually offers when I'm in need of some coding expertise."
t<-read.table(textConnection(string))
string2<-''
i<-0
j<-0
for(i in 1:length(t)){
  if(t[i]=="stackoverflow"){
    j=i
  }else if(j>0){
    if(i-j>4){
      string2=paste0(string2, " " , t[i])
    }
  }else if(j==0){
    if(i>1){
      string2=paste0(string2, " " , t[i])
    }else{
      string2=t[i]
    }
  }
}
print(string2)
Related