Possible Duplicate:
Strip html from strings in python
RegEx match open tags except XHTML self-contained tags
I have the regex pattern in my python module which removes the html tags from the given string.
It doesn’t work for this case.
Input string:
string=<li class="
tal
"><h3><a href="/aclk?sa=l&ai=CoS4y-Wz0TrnqC8y0rAfysK2DB46PiJECzoK8_yKPwd4FCAAQAigCUL7Kz4P9_____wFg5erjg5gOoAH0m_XuA8gBAakCoqvilYNWVD6qBB1P0Dm6CNzrf62IC36fDvUIh77EpeheIRdH_YEaPw&sig=AOD64_2z9xPK8vOxUCpIGTjBcc2Lg-GAeA&adurl=http://www.policybazaar.com/creditcards/creditcard-india.aspx%3Futm_source%3Dgoogle%26utm_medium%3Dppc%26utm_term%3DCreditcard_delhi_only%26utm_campaign%3Dcredit_card" id="pa2">Compare <b>Credit Cards</b> | PolicyBazaar.com</a></h3>Get Best <b>Credit Card</b> For Free, Now U Have a Choice, Choose wisely!<br /><cite>www.policybazaar.com/<b>credit</b>-<b>Cards</b></cite></li>
regex pattern:
In [64]:p = re.compile(r'<.*?>')
In [65]:text=p.sub('',str(string))
In [66]: text
Out[66]: '<li class="\n tal\n ">Compare Credit Cards | PolicyBazaar.comGet Best Credit Card For Free, Now U Have a Choice, Choose wisely!www.policybazaar.com/credit-Cards'
The result has the <li> tag still. How it should be removed irrespective of this class name and string pattern.
In that case you should use the
DOTALLfunctionality:should work.
But… you should not use regexes for HTML parsing, see this: https://stackoverflow.com/a/1732454/11621
HTH.