字符串过滤问题

IT流渊 2010-07-28 11:18:30
字符串过滤问题
有一字符串:
<IMG alt=\"\" hspace=50 src=\"F:\\OLDData\\网页制作知识\\网页制作知识\\导入数据库.jpg\" align=baseline vspace=40 border=0>5235风光好好
<IMG alt=\"\" hspace=50 src=\"F:\\OLDData\\网页制作知识\\网页制作知识\\导入数据库2.jpg\" align=baseline vspace=40 border=0>"5858泰国人天天

想通过过滤得到下面的字符串:
{导入数据库.jpg}5235风光好好{导入数据库2.jpg}5858泰国人天天

想不到好方法~

同志们 帮忙 看看哇~~~~
...全文
329 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
yellowgiutou 2010-07-28
  • 打赏
  • 举报
回复
坐等高手到来
马老虎 2010-07-28
  • 打赏
  • 举报
回复
可以使用正则匹配截取!
zhulong1111 2010-07-28
  • 打赏
  • 举报
回复
Substring+stringbuffer
dengNeeo 2010-07-28
  • 打赏
  • 举报
回复
调用:SqlFilter sf = new SqlFilter();
string str = sf.NoHtml(this.TextBox1.Text.Trim());
开始:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text.RegularExpressions;

/// <summary>
///SqlFilter 的摘要说明
/// </summary>
public class SqlFilter
{
public SqlFilter()
{
//
//TODO: 在此处添加构造函数逻辑
//
}

/// <summary>
/// 过滤标记
/// </summary>
/// <param name="NoHTML">包括HTML,脚本,数据库关键字,特殊字符的源码 </param>
/// <returns>已经去除标记后的文字</returns>
public string NoHtml(string Htmlstring)
{
if (Htmlstring == null)
{
return "";
}
else
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "xp_cmdshell", "", RegexOptions.IgnoreCase);

//删除与数据库相关的词
Htmlstring = Regex.Replace(Htmlstring, "select", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "insert", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "delete from", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "count''", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "drop table", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "truncate", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "asc", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "mid", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "char", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "xp_cmdshell", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "exec master", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "net localgroup administrators", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "and", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "net user", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "or", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "net", "", RegexOptions.IgnoreCase);
//Htmlstring = Regex.Replace(Htmlstring, "*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "-", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "delete", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "drop", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, "script", "", RegexOptions.IgnoreCase);

//特殊的字符
Htmlstring = Htmlstring.Replace("<", "");
Htmlstring = Htmlstring.Replace(">", "");
Htmlstring = Htmlstring.Replace("*", "");
Htmlstring = Htmlstring.Replace("-", "");
Htmlstring = Htmlstring.Replace("?", "");
Htmlstring = Htmlstring.Replace("'", "''");
Htmlstring = Htmlstring.Replace(",", "");
Htmlstring = Htmlstring.Replace("/", "");
Htmlstring = Htmlstring.Replace(";", "");
Htmlstring = Htmlstring.Replace("*/", "");
Htmlstring = Htmlstring.Replace("\r\n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

return Htmlstring;
}
}
}
dalmeeme 2010-07-28
  • 打赏
  • 举报
回复
c中存放源字符串。

string result="{导入数据库.jpg}";
int index=c.IndexOf("5235风光好好");
result+=c.Substring(index,8);
result+="{导入数据库2.jpg}";
index=c.IndexOf("5858泰国人天天");
result+=c.Substring(index,9);

result为最终字符串。
xandercheng 2010-07-28
  • 打赏
  • 举报
回复
等候高手
xinv19shi 2010-07-28
  • 打赏
  • 举报
回复
顶起!!
暖枫无敌 2010-07-28
  • 打赏
  • 举报
回复
我顶
IT流渊 2010-07-28
  • 打赏
  • 举报
回复
呵呵 我也试 用C#语言 发表 不知怎么弄~

非常感谢 逍遥兄 ....
兔子-顾问 2010-07-28
  • 打赏
  • 举报
回复
和大小写无关。我给你的正则忽略大小写的。
兔子-顾问 2010-07-28
  • 打赏
  • 举报
回复
拜托像我这样贴,方便大家阅读。

static void Main(string[] args)
{
string test = "<IMG alt=\"\" hspace=50 src=\"F:\\OLDData\\网页制作知识\\网页制作知识\\导入数据库.jpg\" align=baseline vspace=40 border=0>5235风光好好\r\n<IMG alt=\"\" hspace=50 src=\"F:\\OLDData\\网页制作知识\\网页制作知识\\导入数据库2.jpg\" align=baseline vspace=40 border=0>\"5858泰国人天天";
Console.WriteLine(DoContentText(test));
Console.ReadKey();
}

private static string DoContentText(string fj)
{

if (!fj.Contains("<IMG alt="))
{
return fj;
}
string result = System.Text.RegularExpressions.Regex.Replace(fj, @"(?is)<img.*?src="".+?(?<file>[\w.]+)""[^>]*>(?<text>[^<]+)", delegate(System.Text.RegularExpressions.Match m) { return string.Format("{{{0}}}{1}", m.Groups["file"].Value, m.Groups["text"].Value); });

return result;
}

结果:
{导入数据库.jpg}5235风光好好
{导入数据库2.jpg}"5858泰国人天天


------------------------------------------------------------------

以上代码都是运行测试过,。你贴进去一定有结果的。你如果测试过我给你的,你不会回复说没结果。如果说不对,像我这样给出完整测试例子。否则谁猜得到你的问题是什么?
IT流渊 2010-07-28
  • 打赏
  • 举报
回复
恩找到原因了 )<img.*?src="".+?(?<

我的字符串里 img是大写的 把 它改成大写 就ok 了 谢谢~~
兔子-顾问 2010-07-28
  • 打赏
  • 举报
回复
调用的也贴来。才知道你用什么数据测试出没结果的。
IT流渊 2010-07-28
  • 打赏
  • 举报
回复
另外 我的vs2008的
IT流渊 2010-07-28
  • 打赏
  • 举报
回复
private string DoContentText(string fj)
{

if (!fj.Contains("<IMG alt="))
{
return fj;
}
string result = System.Text.RegularExpressions.Regex.Replace(fj, @"(?is)<img.*?src="".+?(?<file>[\w.]+)""[^>]*>(?<text>[^<]+)",delegate(System.Text.RegularExpressions.Match m){return string.Format("{{{0}}}{1}", m.Groups["file"].Value, m.Groups["text"].Value);});

return result;
}
这个是我的处理函数
chunquanwang 2010-07-28
  • 打赏
  • 举报
回复
mark
兔子-顾问 2010-07-28
  • 打赏
  • 举报
回复
你要贴你的例子来。你用我给你的例子没结果么?你代码贴来。
IT流渊 2010-07-28
  • 打赏
  • 举报
回复
代码 没问题
就是没起作用
IT流渊 2010-07-28
  • 打赏
  • 举报
回复
不好意意思
delegate(Match m)
{
return string.Format("{{{0}}}{1}", m.Groups["file"].Value, m.Groups["text"].Value);
});
这个问题
就是 字符串经过 处理
出来 没有变化
一进一出 没有变化
兔子-顾问 2010-07-28
  • 打赏
  • 举报
回复
在哪里用的js? vs.net?什么版本?2005?2008?
加载更多回复(5)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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