asp.net dirty dictionary filtering problem using regular expressions to filter dirty data _ practical Tips

Source: Internet
Author: User
Tags readline
Method One: Use regular expressions
Copy Code code as follows:

Dirty dictionary data store file path
private static string File_name= "Zang.txt";
Dirty data dictionary table, such as: Dirty data One | Dirty data two | dirty data three
public static string Dirtystr= "";

Public Validdirty ()
{
if (httpruntime.cache["Regex"]==null)
{
Dirtystr=readdic ();
Regular expression used to detect dirty dictionaries
Regex validatereg= The new Regex ("^ (?!") +dirtystr+ "). (? <! " +dirtystr+ ")) *$", regexoptions.compiled|regexoptions.explicitcapture);
HttpRuntime.Cache.Insert ("Regex", Validatereg,null,datetime.now.addminutes (), TimeSpan.Zero);
}

}
private String Readdic ()
{
file_name=environment.currentdirectory+ "\" +file_name;

if (! File.exists (file_name))
{
Console.WriteLine ("{0} does not exist.", file_name);
Return "";
}
StreamReader sr = File.OpenText (file_name);
String input= "";
while (Sr. Peek () >-1)
{
input = Sr. ReadLine ();
}

Sr. Close ();
return input;

}


public bool Validbyreg (string str)
{
Regex reg= (regex) httpruntime.cache["regex"];
Return Reg. IsMatch (str);

}

Feel this method of execution is not very high, simple test 1000 words of the article, Dirty Dictionary has more than 800 keywords
The formula is 1.238 seconds, we have no better way, please do not hesitate to enlighten!

Method Two: General circulation search method
Copy Code code as follows:

public bool Validgeneral (string str)
{

if (! File.exists (file_name))
{
Console.WriteLine ("File path or file path does not have error message");
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;

}

}

The following is the test method, what is the problem please point out!
Copy Code code as follows:

DateTime T1 =datetime.now;
String str= "213";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
Validdirty vd=new Validdirty ();
Console.WriteLine (VD. Validbyreg (str));
DateTime T2 =datetime.now;
TimeSpan ts=t2-t1;
Console.WriteLine (TS. TotalMilliseconds);
Console.read ();

Algorithm

Retrieving text file length/time consuming (MS)

Regular algorithm

10 Chinese Characters/980

100 Chinese Characters/999

1000 Chinese Characters/1234

Common algorithm

10 Chinese characters/234

100 Chinese Characters/234

1000 Chinese Characters/265

Dirty Dictionary Download
However, this article may have some problems, suggest to look at the following 2 articles before deciding to use.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.