I am trying to make the correct REGEX to complete this task:
Match the Nth word of a string containing a specific word
For instance:
Input:
this is the first line - blue this is the second line - green this is the third line - red
I want to match the word 7th of the lines containing the word " second "
Required Conclusion:
green
Does anyone know how to do this?
I use http://rubular.com/ to test REGEX.
I already tried this REGEX without success - it matches the next line
(.*second.*)(?<data>.*?\s){7}(.*)
--- UPDATED ---
Example 2
Input:
this is the Foo line - blue this is the Bar line - green this is the Test line - red
I want to match the word 4th lines containing the word " red "
Required Conclusion:
Test
In other words, the word I want to match can be obtained before or after the word that I use to select the line
Jorge source share