string - How to get the text between two words in R? -


i trying text between 2 words in sentence.
example sentence -

x <-  "this first sentence" 

now want text between this , first is my . have tried various functions r grep, grepl, pmatch , str_split. however, not want .

this closest have reached gsub.

gsub(".*this\\s*|first*", "", x) 

the output gives

 [1] "is  sentence" 

in reality, need

[1] "is my" 

any appreciated.

you need .* @ end match 0 or more characters after 'first'

 gsub('^.*this\\s*|\\s*first.*$', '', x)  #[1] "is my" 

Comments