设为首页 加入收藏 网站搜索 繁體中文 中国建站网 — 站长资源分享平台

关于脏字典过滤问题-用正则表达式来过滤脏数据

来源:网络收集 作者:佚名 时间:2007-04-25 23:24:47

方法一:使用正则表达式

 1//脏字典数据存放文件路径
 2        private static string FILE_NAME="zang.txt";
 3        //脏数据字典表,如:脏数据一|脏数据二|脏数据三
 4        public static string dirtyStr="";
 5       
 6        public ValidDirty()
 7        {
 8            if (HttpRuntime.Cache["Regex"]==null)
 9            {
10                dirtyStr=ReadDic();
11                //用于检测脏字典的正则表达式
12                Regex validateReg= new Regex("^((?!"+dirtyStr+").(?<!"+dirtyStr+"))*$",RegexOptions.Compiled|RegexOptions.ExplicitCapture);   
13                HttpRuntime.Cache.Insert("Regex" ,validateReg,null,DateTime.Now.AddMinutes(20) ,TimeSpan.Zero);
14            }
15           
16        }
17        private string ReadDic()
18        {
19            FILE_NAME=Environment.CurrentDirectory+"\\"+FILE_NAME;
20
21            if (!File.Exists(FILE_NAME))
22            {
23                Console.WriteLine("{0} does not exist.", FILE_NAME);
24                return "";
25            }
26            StreamReader sr = File.OpenText(FILE_NAME);
27            String input="";
28            while (sr.Peek() > -1)
29            {
30                input += sr.ReadLine() ;
31            }
32           
33            sr.Close();
34            return input;
35
36        }
37
38       
39        public bool ValidByReg(string str)
40        {
41            Regex reg=(Regex)HttpRuntime.Cache["Regex"];
42            return reg.IsMatch(str) ;
43           
44        }

感觉这种方法的执行效率不是很高,简单的测试了一下 1000字的文章,脏字典有800多个关键字
式了一下是 1.238秒,大家有没有更好的方法,请不吝赐教!

方法二:普通循环查找方法

    public bool ValidGeneral(string str)
        {
           
            if(!File.Exists(FILE_NAME))
            {
                Console.WriteLine("文件路径或者文件路径不存在错误信息") ;
                return false;
            }
            else
            {
                StreamReader objReader = new StreamReader(FILE_NAME,System.Text.Encoding.GetEncoding("gb2312"));
                string sLine="";

[1] [2] [3] [4]  下一页

Tags:

  • 好的评价 如果您觉得好,就请您
      0%(0)
  • 差的评价 如果您觉得差,就请您
      0%(0)
  • 相关文章
    广告赞助

    文章随便看看 设计素材 建站学院 网页模板 视频教程

    网友评论

    共有 0 位网友发表了评论,得分 0 分,平均 0 分    查看完整评论

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 1分

    内 容:

             通知管理员 验证码: