Python Regular Expression for a paragraph
<p>Hi i have this as my testing string:</p>

<pre><code>&lt;image&gt;
&lt;title&gt;CNN.com - Technology&lt;/title&gt;
&lt;link&gt;http://www.cnn.com/TECH/index.html?eref=rss_tech&lt;/link&gt;
</code></pre>

<p>and i want to select 'Technology' from it using a python regular expression, however i need it specific so that it uses <code>&lt;image&gt;</code> and <code>&lt;link&gt;</code>. So far the expression i have is:</p>

<pre><code>'&lt;title[^&gt;]*&gt;CNN.com - (.*?)&lt;/title&gt;'
</code></pre>

<p>this expression works to select 'Technology', this is correct however i am unsure how to specialise my code using  <code>&lt;image&gt;</code> and <code>&lt;link&gt;</code> in the expression. For example i need something along the lines of this regular expression <code>'&lt;image&gt;&lt;title[^&gt;]*&gt;CNN.com - (.*?)&lt;/title&gt;&lt;link&gt;'</code> that would actually work to produce the same result of 'Technology?  </p>

<p>Your regexp is not bad but you need to escape the slash in <code>&lt;/title&gt;</code> with a backslash and it does not match because of the newlines in your string.</p>

<p>Newlines are whitespaces (like space, tabulation... \s is equivalent to [ \t\n\r\f\v] when the UNICODE flag is not set), so you can use \s to match them.</p>

<p>I assume you're using python3 but it does not matter.</p>

<pre><code>s = """&lt;image&gt;
&lt;title&gt;CNN.com - Technology&lt;/title&gt;
&lt;link&gt;http://www.cnn.com/TECH/index.html?eref=rss_tech&lt;/link&gt;"""
r = r"&lt;image&gt;[\s]*&lt;title[^&gt;]*&gt;CNN.com - (.*?)&lt;\/title&gt;[\s]*&lt;link&gt;"
m = re.search(r, s)
print(m.group(0))
print(m.group(1))
</code></pre>

<p>group(1) is "Technology".</p>

python regex