Extract string based on position of character found in R

I found the provisions of "oo" in the following sentence:

sentence <- "It is a good book. Good for first reading. This book explains everything in Qdetail with tons of examples and exercises for practice. Good for cracking written tests on campuses and competitive exams. It is cheap so any way one can have a copy along with other books" pos = gregexpr("oo", sentence) 

I got the result as

 > pos [[1]] [1] 10 15 21 50 136 263 attr(,"match.length") [1] 2 2 2 2 2 2 attr(,"useBytes") [1] TRUE 

Based on the result, I want to extract 10 characters from each position (5 to position and 5 after position)

For example, I have to get the result for the first location, because "good bo" And I want this booty for each position. Since I am new to R, I could not figure out how to do this. Please help me with this.

What if I need to extract the words as shown below: I have to get a โ€œgood bookโ€ for the first copy of the match

+5
source share
4 answers

We can use substring after unlist output.

 v1 <- unlist(gregexpr("oo", sentence)) substring(sentence, v1 - 5, v1 +5) #[1] "sa good bo" "ood book. G" "ok. Good fo" "his book ex" "ce. Good fo" "her books" 
+8
source

You can also do

 mapply( substr, x=sentence, start=pos[[1]]-5, stop=pos[[1]]+5, USE.NAMES = F ) # [1] "sa good bo" "ood book. G" "ok. Good fo" # [4] "his book ex" "ce. Good fo" "her books" 
+5
source

If you do not mind using the stringr package

 library(stringr) ind<-str_locate_all(sentence,"oo")[[1]][ ,1] str_sub(sentence,ind-5,ind+5) [1] "sa good bo" "ood book. G" "ok. Good fo" "his book ex" "ce. Good fo" "her books" 
+4
source
 pos = gregexpr("oo", sentence) 

Adjust position and length:

 pos[[1]] <- pmax(pos[[1]] - 5, 0) attr(pos[[1]], "match.length")[] <- 11 regmatches(sentence, pos) #[[1]] #[1] "sa good bo" "ood book. G" "ok. Good fo" "his book ex" "ce. Good fo" "her books" 
+4
source

All Articles