利用正则表达式去掉html代码

using System.Text.RegularExpressions;//需要引用
None.gif
None.gif  // 利用正则表达式去掉"<"和">"之间的内容
None.gif  private string StripHT(string strHtml)
ExpandedBlockStart.gif ContractedBlock.gif   dot.gif{
InBlock.gif    Regex regex=new Regex("<.+?>",RegexOptions.IgnoreCase);
InBlock.gif   string strOutput=regex.Replace(strHtml,"");
InBlock.gif   return strOutput;
ExpandedBlockEnd.gif   }
None.gif
None.gif
None.gif//方法二(不知为什么此方法占用CPU100%)
None.gif
None.gifpublic static string DropHTML(string strHtml)
ExpandedBlockStart.gif ContractedBlock.gif   dot.gif{
ExpandedSubBlockStart.gif ContractedSubBlock.gif   string [] aryReg = dot.gif{
InBlock.gif          @"<script[^>]*?>.*?</script>",
InBlock.gif          @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""''])(\\[""''tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
InBlock.gif          @"([\r])[\s]+",
InBlock.gif          @"&(quot|#34);",
InBlock.gif          @"&(amp|#38);",
InBlock.gif          @"&(lt|#60);",
InBlock.gif          @"&(gt|#62);",
InBlock.gif          @"&(nbsp|#160);",
InBlock.gif          @"&(iexcl|#161);",
InBlock.gif          @"&(cent|#162);",
InBlock.gif          @"&(pound|#163);",
InBlock.gif          @"&(copy|#169);",
InBlock.gif          @"&#(\d+);",
InBlock.gif          @"-->",
InBlock.gif          @"<!--.*"         
ExpandedSubBlockEnd.gif          };
InBlock.gif
ExpandedSubBlockStart.gif ContractedSubBlock.gif   string [] aryRep = dot.gif{
InBlock.gif           "",
InBlock.gif           "",
InBlock.gif           "",
InBlock.gif           "\"",
InBlock.gif           "&",
InBlock.gif           "<",
InBlock.gif           ">",
InBlock.gif           " ",
InBlock.gif           "\xa1",//chr(161),
InBlock.gif           "\xa2",//chr(162),
InBlock.gif           "\xa3",//chr(163),
InBlock.gif           "\xa9",//chr(169),
InBlock.gif           "",
InBlock.gif           "\r",
InBlock.gif           ""    
ExpandedSubBlockEnd.gif           };
InBlock.gif
InBlock.gif   string newReg =aryReg[0];
InBlock.gif   string strOutput=strHtml;
InBlock.gif   for(int i = 0;i<aryReg.Length;i++)
ExpandedSubBlockStart.gif ContractedSubBlock.gif    dot.gif{
InBlock.gif     Regex regex = new Regex(aryReg[i],RegexOptions.IgnoreCase );
InBlock.gif     strOutput = regex.Replace(strOutput,aryRep[i]);
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif    strOutput.Replace("<","");
InBlock.gif    strOutput.Replace(">","");
InBlock.gif    strOutput.Replace("\r","");
InBlock.gif   return strOutput;
InBlock.gif      
ExpandedBlockEnd.gif   }
None.gif
None.gif http://www.cnblogs.com/wang123/archive/2007/09/19/898668.html

转载于:https://www.cnblogs.com/xinzhuangzi/archive/2009/10/03/4100665.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值