日期:2014-05-17  浏览次数:21081 次

求一正则表达式,急用~~
要求将html代码中的所有<!--[if gte vml 1]><![endif]-->标签中的内容去掉
并将所有<![if !vml]><![endif]>外层标签去掉,但保留标签里面的内容

比如以下html代码


<p class=MsoNormal><span lang=EN-US><!--[if gte vml 1]><v:shapetype id="_x0000_t75"
 coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe"
 filled="f" stroked="f">
 <v:stroke joinstyle="miter"/>
 <v:formulas>
  <v:f eqn="if lineDrawn pixelLineWidth 0"/>
  <v:f eqn="sum @0 1 0"/>
  <v:f eqn="sum 0 0 @1"/>
  <v:f eqn="prod @2 1 2"/>
  <v:f eqn="prod @3 21600 pixelWidth"/>
  <v:f eqn="prod @3 21600 pixelHeight"/>
  <v:f eqn="sum @0 0 1"/>
  <v:f eqn="prod @6 1 2"/>
  <v:f eqn="prod @7 21600 pixelWidth"/>
  <v:f eqn="sum @8 21600 0"/>
  <v:f eqn="prod @7 21600 pixelHeight"/>
  <v:f eqn="sum @10 21600 0"/>
 </v:formulas>
 <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/>
 <o:lock v:ext="edit" aspectratio="t"/>
</v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" style='width:383.25pt;
 height:169.5pt'>
 <v:imagedata src="intro.files/image001.jpg" o:title="class"/>
</v:shape><![endif]--><![if !vml]><img width=511 height=226
src="intro.files/image001.jpg" v:shapes="_x0000_i1025"><![endif]></span></p>


经过正则过滤后变为

<p class=MsoNormal><span lang=EN-US><img width=511 height=226
src="intro.files/image001.jpg" v:shapes="_x0000_i1025"></span></p>


求大神指点。


------解决方案--------------------
 string str = "字符串";
            str = Regex.Replace(str, @"(?is)<!\[(if !vml
------解决方案--------------------
endif)\]>
------解决方案--------------------
<!\-\-\[if gte vml 1\]>.*?<!\[endif\]\-\->", "");
           

------解决方案--------------------

try:
File.WriteAllText(context.Server.MapPath("~/uploads/model/") + fname + ".htm", strContent,Encoding.Default);

------解决方案--------------------
你读取的时候没指定Encoding
另外,你可以试试这个正则

(?s)<!--.+?-->
------解决方案--------------------
<![^>]*>