Extract specific string part from HTML content with Regex
<p>I'm trying to extract with REGEX from the following string:</p>

<p><code>&lt;tr class="data"&gt;&lt;td class="first"&gt;&lt;a href="en/clinics/al-khayal-medical-centre-6d15db1f.aspx"&gt;AL KHAYAL MEDICAL CENTRE&lt;/a&gt;&lt;/td&gt;&lt;td class="second not-responsive"&gt;&lt;a href="/portal/en/healthcare/clinics.aspx?speciality=Urology"&gt;Urology&lt;/a&gt;&lt;/td&gt;&lt;td class="third"&gt;&lt;label style="display:block ;direction:ltr; text-align:left"&gt;042765600&lt;/label&gt;&lt;/td&gt;&lt;td class="fourth"&gt;&lt;a href="http://www.alkhayalmedicalcentre.com" target="_blank"&gt;www.alkhayalmedicalcentre.com&lt;/a&gt;&lt;/td&gt;&lt;td class="fifth not-responsive"&gt;&lt;a class="clinic-details btn noborder basic small" href="en/clinics/al-khayal-medical-centre-6d15db1f.aspx" rel="1dfd7cf2-49c8-43eb-80b8-9393c9f48ca4"&gt;More Info&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;</code></p>

<p>I'm trying to extract just the following part: </p>

<pre><code>en/clinics/almavita-medical-clinic-fz-llc.aspx    
</code></pre>

<pre><code>import re

html = '''
  &lt;tr class="data"&gt;&lt;td class="first"&gt;&lt;a href="en/clinics/al-khayal-
  medical-centre-6d15db1f.aspx"&gt;AL KHAYAL MEDICAL CENTRE&lt;/a&gt;&lt;/td&gt;
  &lt;td class="second not-responsive"&gt;&lt;a href="/portal/en/healthcare/clinics.aspx?speciality=Urology"&gt;Urology&lt;/a&gt;&lt;/td&gt;&lt;td class="third"&gt;
  &lt;label style="display:block ;direction:ltr; text-align:left"&gt;042765600&lt;/label&gt;&lt;/td&gt;&lt;td class="fourth"&gt;
  &lt;a href="http://www.alkhayalmedicalcentre.com" target="_blank"&gt;www.alkhayalmedicalcentre.com&lt;/a&gt;
  &lt;/td&gt;&lt;td class="fifth not-responsive"&gt;
  &lt;a class="clinic-details btn noborder basic small" href="en/clinics/al-khayal-medical-centre-6d15db1f.aspx" rel="1dfd7cf2-49c8-43eb-80b8-9393c9f48ca4"&gt;More Info&lt;/a&gt;
  &lt;/td&gt;&lt;/tr&gt;
'''

pat = re.compile(r"a href=(\".*?\")")
# for being more specific can use this regex:
# pat = re.compile(r"&lt;a.*?href=(\".*?\").*?&gt;")

# retrieves all links
print (re.findall(pat, html))
</code></pre>

<p>will give:</p>

<pre><code>[ '"en/clinics/al-khayal-medical-centre-6d15db1f.aspx"',
  '"/portal/en/healthcare/clinics.aspx?speciality=Urology"', 
  '"http://www.alkhayalmedicalcentre.com"']
</code></pre>

<p>so, do:</p>

<pre><code># return the first occurring link
print (re.findall(pat, html)[0])
</code></pre>

<p>output:</p>

<pre><code>"en/clinics/al-khayal-medical-centre-6d15db1f.aspx"
</code></pre>

python regex python-2.7 html-parsing