您的位置:首页 >> Web开发 >> ASP.NET >> ASP.Net技巧 >> 正文
RSS
 

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

http://www.rdxx.com 06年09月13日 00:00 我要投稿

关键词: 正则表达式 , 问题 , 字典 , 过滤 , 数据 , , 正则

方法一:使用正则表达式

 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        

上一页 下一页

 
 
标签: 正则表达式 , 问题 , 字典 , 过滤 , 数据 , , 正则 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站