title: How to match the Nth word of a line containing a specific word using regex
body: <p>I'm trying to do to get the correct regular expression to match the Nth word of a line containing a specific word.</p>
  
  <p>For example, if I have this input:</p>
  
  <pre><code>this is the first line - blue
  this is the second line - green
  this is the third line - red
  </code></pre>
  
  <p>I want to match the seventh word of the lines containing the word "second" and return <code>green</code>.</p>
  
  <p>I'm using <a href="http://rubular.com/" rel="nofollow noreferrer">Rubular</a> to test the regular expression.</p>
  
  <p>I already tried out this regular expression without success - it is matching the next line:</p>
  
  <pre><code>(.*second.*)(?&lt;data&gt;.*?\s){7}(.*)
  </code></pre>
  
  <p>Another example input:</p>
  
  <pre><code>this is the Foo line - blue
  this is the Bar line - green
  this is the Test line - red
  </code></pre>
  
  <p>I want to match the fourth word of the lines containing the word "red" and return <code>Test</code>.</p>
  
  <p>The word I want to match can come either <em>before</em> or <em>after</em> the word I use to select the line.</p>