Find a substring in block of text, unless it is part of another substring
<p>I was looking for an efficient way to find a substring between two expressions, unless the expression is a part of another.</p>

<p>For example:</p>

<blockquote>
  <p>Once upon a time, in a time far far away, dogs ruled the world. The End.</p>
</blockquote>

<p>If I was searching for the substring between <strong>time</strong> and <strong>end</strong>, I would receive:</p>

<blockquote>
  <p>in a time far far away, dogs ruled the world. The</p>
</blockquote>

<p>or </p>

<blockquote>
  <p>far far away, dogs ruled the world. The</p>
</blockquote>

<p>I want to ignore if <strong>time</strong> is a part of <strong>Once upon a time</strong>. I didn't know if there was a pythonic method without using crazy for loops and if/else cases.</p>

<p>This is possible in regex by using a negative lookahead</p>

<pre><code>&gt;&gt;&gt; s = 'Once upon a time, in a time far far away, dogs ruled the world. The End.'
&gt;&gt;&gt; pattern = r'time((?:(?!time).)*)End'
&gt;&gt;&gt; re.findall(pattern, s)
[' far far away, dogs ruled the world. The ']
</code></pre>

<p>With multiple matches:</p>

<pre><code>&gt;&gt;&gt; s = 'a time b End time c time d End time'
&gt;&gt;&gt; re.findall(pattern, s)
[' b ', ' d ']
</code></pre>

python python-2.7