Capturing text with Python regular expressions
<p>I've been having a bit of trouble with capturing strings between html tags using Python regular expressions. I've been trying to capture the string "example link 2" from the string below:</p>

<pre><code>&lt;link&gt;example link 1&lt;/link&gt;
&lt;item&gt;
     &lt;link&gt;example link 2&lt;/link&gt;
&lt;/item&gt;
</code></pre>

<p>I've got this so far:</p>

<pre><code>(?&lt;=&lt;link&gt;)(.*)(?=&lt;/link&gt;)
</code></pre>

<p>However the regular expression above returns "example link 1" and "example link 2". Could anyone please help with selecting only "example link 2"? </p>

<p>EDIT: Unfortunately I'm required to use regular expressions for this question so i can't use a parser etc. Thanks for the recommendation though.</p>

<p>You need to add 'g' modifier at the end. For example the regex should look like:  </p>

<pre><code>/(?&lt;=\&lt;link&gt;)(.*)(?=&lt;\/link&gt;)/g
</code></pre>

<p>The 'g' modifier tells the engine not to stop after the first match has been found, but rather to continue until no more matches can be found.<br>
Demo <a href="https://regex101.com/r/oF9eM7/2" rel="nofollow">here</a></p>

python regex python-2.7