首页 > 开发 > 综合 > 正文

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

2024-07-21 02:28:37
字体:
来源:转载
供稿:网友

方法一:使用正则表达式

 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="";
                arraylist arrtext = new arraylist();

                while (sline != null)
                {
                    sline = objreader.readline();
                    if (sline != null)
                        arrtext.add(sline);
                   
                }
                objreader.close();


                foreach (string soutput in arrtext)
                {
                    string[] strarr=soutput.split('|');
                   
                    for (int i = 0; i < strarr.length; i++)
                    {
                        if (str.indexof(strarr[i])!=-1)
                        {
                            return false;   
                        }
                       
                    }
                   
                }
                return true;

            }

        }

以下是测试的方法,有什么问题还大家请指出!

 1datetime t1 =datetime.now;
 2            string str="213";
 3            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
 4            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
 5            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
 6            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
 7            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
 8            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
 9            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
10            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
11            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
12            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";   
13            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
14            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
15            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
16            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
17            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
18            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
19            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
20            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
21            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
22            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
23            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
24            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
25            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
26            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
27            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
28            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
29            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
30            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
31            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
32            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
33            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
34            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
35            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
36            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
37            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
38            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
39            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
40            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
41            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
42            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
43            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
44            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
45            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
46            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
47            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
48            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
49            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
50            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
51            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
52            str+="珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋珍惜水晶之恋";
53            validdirty vd=new validdirty() ;
54            console.writeline(vd.validbyreg(str))  ;
55            datetime t2 =datetime.now;
56            timespan ts=t2-t1;
57            console.writeline(ts.totalmilliseconds) ;
58            console.read() ;

算法

检索文本文件长度 / 耗费时间(ms

正则算法

10个汉字/ 980

100个汉字/999

1000个汉字/1234

普通算法

10个汉字/ 234

100个汉字/234

1000个汉字/265

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表