Asp.net dirty dictionary Filtering Problems use regular expressions to filter dirty data

Source: Internet
Author: User

Method 1: Use a regular expression
Copy codeThe Code is as follows:
// Path for storing dirty Dictionary data
Private static string FILE_NAME = "zang.txt ";
// Dirty data dictionary table, such as: dirty data 1 | dirty data 2 | dirty data 3
Public static string dirtyStr = "";

Public ValidDirty ()
{
If (HttpRuntime. Cache ["Regex"] = null)
{
DirtyStr = ReadDic ();
// Regular expression used to detect dirty dictionaries
Regex validateReg = new Regex ("^ ((?! "+ DirtyStr + ").(? <! "+ DirtyStr +") * $ ", RegexOptions. Compiled | RegexOptions. ExplicitCapture );
HttpRuntime. Cache. Insert ("Regex", validateReg, null, DateTime. Now. AddMinutes (20), 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 );

}

I feel that the execution efficiency of this method is not very high. I tested the article with 1000 words, and the dirty dictionary has more than 800 keywords.
It takes 1.238 seconds. Do you have any better solutions!

Method 2: Common loop search
Copy codeThe Code is as follows:
Public bool ValidGeneral (string str)
{

If (! File. Exists (FILE_NAME ))
{
Console. WriteLine ("the file path or file path does not have an 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. If you have any questions, please point out it!
Copy codeThe Code is as follows:
DateTime t1 = DateTime. Now;
String str = "213 ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
Str + = "Cherish the love of the crystal, cherish the love of the crystal, and cherish the love of the Crystal ";
ValidDirty vd = new ValidDirty ();
Console. WriteLine (vd. ValidByReg (str ));
DateTime t2 = DateTime. Now;
Time Span ts = t2-t1;
Console. WriteLine (ts. TotalMilliseconds );
Console. Read ();

Algorithm

Retrieving text file length/elapsed time (MS)

Regular Algorithm

10 Chinese characters/980

100 Chinese characters/999

1000 Chinese characters/1234

Common Algorithms

10 Chinese characters/234

100 Chinese characters/234

1000 Chinese characters/265

Dirty dictionary download
However, this article may have some problems. It is recommended that you read the following two articles before deciding to use them.

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.