title: First lookahead then look for closest matching capture group behind the lookahead match. RegEx in Python
body: <p>I have a full text with line separated strings. Lines starting with '%' are titles and lines starting with '>' contain the text I want to look for my my query in. If my query is found I want to return the nearest title above it. Here is the expression I tried myself:</p>
  
  <pre><code>import re
  query = "ABCDE"
  full_text = "%EFGHI\r&gt;XXXXX\r&gt;XXXXX\r%IWANT\r&gt;XXXXX\r&gt;ABCDE"
  re.search("%(.*?)\r(?=&gt;.*{})".format(query), full_text).group(0)
  </code></pre>
  
  <p>I want this code block to return the string:</p>
  
  <pre><code>&gt; 'IWANT'
  </code></pre>
  
  <p>As this is the closest title above the query. However, it returns:</p>
  
  <pre><code>&gt; 'EFGHI'
  </code></pre>
  
  <p>I guess it makes sense, since 'EFGHI' is the first element matching the search pattern. Is there a way to first lookahead for my query and then look back for the nearest title?</p>