正则表达式

dahaig 2006-08-21 06:13:39
下面是网上下载的一段去除字符串中html标记的代码
但是应用在这个字符串中却不行
<FONT color=#0000ff>qweqweqwe</FONT>123123123
得到<FONT color=#0000ff>qweqweqwe123123123

这里的代码格式看起来有点费劲
大家可以在这看,我就是从这下载的
http://bbs.ntc.com.cn/csharp/csharp_35417.htm

请高手帮修改下,项目紧急,没有时间让我研究正则表达式了,谢谢!!

public static string StripHTML(string strHtml)
{
string [] aryReg ={
@"<script[^>]*?>.*?</script>",

@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
@"([\r\n])[\s]+",
@"&(quot|#34);",
@"&(amp|#38);",
@"&(lt|#60);",
@"&(gt|#62);",
@"&(nbsp|#160);",
@"&(iexcl|#161);",
@"&(cent|#162);",
@"&(pound|#163);",
@"&(copy|#169);",
@"&#(\d+);",
@"-->",
@"<!--.*\n"

};

string [] aryRep = {
"",
"",
"",
"\"",
"&",
"<",
">",
" ",
"\xa1",//chr(161),
"\xa2",//chr(162),
"\xa3",//chr(163),
"\xa9",//chr(169),
"",
"\r\n",
""
};

string newReg =aryReg[0];
string strOutput=strHtml;
for(int i = 0;i<aryReg.Length;i++)
{
Regex regex = new Regex(aryReg[i],RegexOptions.IgnoreCase );
strOutput = regex.Replace(strOutput,aryRep[i]);
}

strOutput.Replace("<","");
strOutput.Replace(">","");
strOutput.Replace("\r\n","");


return strOutput;
}
...全文
192 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dahaig 2006-08-22
  • 打赏
  • 举报
回复
谢谢!
Yellow198267 2006-08-22
  • 打赏
  • 举报
回复
关注
www_123du_com 2006-08-21
  • 打赏
  • 举报
回复
Regex.Replace(strAll,@"(?:<script.*?>.*?</script>)|(?:<.*?>)","",RegexOptions.IgnoreCase);
ojekleen 2006-08-21
  • 打赏
  • 举报
回复
欢迎加个.net成长群:21560447
idda 2006-08-21
  • 打赏
  • 举报
回复
楼上写成楼主了:-(,楼主的正则表达式看起来太费尽
楼主可以在原来的基础上再去除一次
string pattern = @"<FONT color=.*>";
strOutput = Regex.Replace(strOutput, pattern, string.Empty);


idda 2006-08-21
  • 打赏
  • 举报
回复
FilterScript是个什么函数?是楼主自己的函数吗?
kevin_gao 2006-08-21
  • 打赏
  • 举报
回复
要不简单点用
public static string RemoveHtml(string content)
{
string newstr=FilterScript(content);
string regexstr=@"<[^>]*>";
return Regex.Replace(newstr,regexstr,string.Empty,RegexOptions.IgnoreCase);
}

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧