title: How to get the text between two words in R?
body: <p>I am trying to get the text between two words in a sentence.<br>
    For example the sentence is - </p>
  
  <pre><code>x &lt;-  "This is my first sentence"
  </code></pre>
  
  <p>Now I want the text between <code>This</code> and <code>first</code> which is <code>is my</code> . 
  I have tried various functions from R like <code>grep</code>, <code>grepl</code>, <code>pmatch</code> , <code>str_split</code>. However, I could not get exactly what I want .   </p>
  
  <p>This is the closest what I have reached with <code>gsub</code>. </p>
  
  <pre><code>gsub(".*This\\s*|first*", "", x)
  </code></pre>
  
  <p>The output it gives is     </p>
  
  <pre><code> [1] "is my  sentence"
  </code></pre>
  
  <p>In reality, what I need is only </p>
  
  <pre><code>[1] "is my"
  </code></pre>
  
  <p>Any help would be appreciated. </p>