Regex to remove conditional comments

Posted by cnu on Stack Overflow See other posts from Stack Overflow or by cnu
Published on 2008-09-25T10:23:33Z Indexed on 2010/04/15 11:23 UTC
Read the original article Hit count: 476

Filed under:
|

I want a regex which can match conditional comments in a HTML source page so I can remove only those. I want to preserve the regular comments.

I would also like to avoid using the .*? notation if possible.

The text is

foo

<!--[if IE]>

<style type="text/css">

ul.menu ul li{
    font-size: 10px;
    font-weight:normal;
    padding-top:0px;
}

</style>

<![endif]-->

bar

and I want to remove everything in <!--[if IE]> and <![endif]-->

EDIT: It is because of BeautifulSoup I want to remove these tags. BeautifulSoup fails to parse and gives an incomplete source

EDIT2: [if IE] isn't the only condition. There are lots more and I don't have any list of all possible combinations.

EDIT3: Vinko Vrsalovic's solution works, but the actual problem why beautifulsoup failed was because of a rogue comment within the conditional comment. Like

<!--[if lt IE 7.]>
<script defer type="text/javascript" src="pngfix_253168.js"></script><!--png fix for IE-->
<![endif]-->

Notice the <!--png fix for IE--> comment?

Though my problem was solve, I would love to get a regex solution for this.

© Stack Overflow or respective owner

Related posts about python

Related posts about regex