015 days (.Net knowledge learning path -C#) - regular expressions (UBB translati

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

A translation, UBB

  In order to prevent users from using the malicious code message, most of the forum are not allowed to use the HTML statement to return card content (or escaped), but this limits the user to play, such as user cannot paste a hyperlink, you cannot paste pictures address, cannot send myself text bold, change the font size. In order to solve this problem, people design the UBB code, it provides limited, and no harm, as long as the user follows the code rules can achieve users want to function.

  UBB: StringReplace


Two, sensitive word processing

Direct example (the old is harmonious, let brother also harmonious a)

Example: 1, do a WinForm page, a multi line text box, click on the button on the contents of the text box as a post judgment. If the "filter words 1.TXT" in the text, it is prohibited from sending processing.

Filter words:

The sale of hand. The gun={BANNED}

The sale of bank card={BANNED}

Fax={BANNED}

Aphrodisiac={BANNED}

Network promotion={MOD}

Network marketing={MOD}

Online processing={BANNED}

Online booking air tickets={BANNED}

…………………………


 1  private void button1_Click(object sender, EventArgs e)
 2         {
 3             //Definition two collections were accepted to prohibit sending content and contents need to be examined
 4             List<string> mod = new List<string>();
 5             List<string> banned = new List<string>();
 6             //Read, pay attention to read when not garbled
 7             string[] str = File.ReadAllLines(@"d:\1.txt", Encoding.Default);
 8            //Traversing arrays and matching, the matching results are added to the collection
 9             foreach (string item in str)
10             {
11                 Match match = Regex.Match(item, "(.+)={(.+)}");
12                 if (match.Success)
13                 {
14                     if (match.Groups[2].ToString() == "MOD")
15                     {
16                         mod.Add(match.Groups[1].ToString());
17                     }
18                     else
19                     {
20                         banned.Add(match.Groups[1].ToString());
21                     }
22                 }
23             }
24             //By banning the transmission as an example
25             //The first method, traversing the banned array, if the input text contains the contents of the collection is prohibited from sending
26             //foreach (string item in banned)
27 
28             //{
29             //    if (textBox1.Text.Contains(item))
30             //    {
31             //        MessageBox.Show("Do not send");
32             //        return;
33             //    }
34             //else
35             //{
36             //    MessageBox.Show("Send success");
37             //    return;
38             //}
39 
40             //}
41 
42             //The second method, the contents of the collection into an array and use “ |” segmentation, as regular expressions
43             string reg = string.Join("|", banned.ToArray());
44             Match match2 = Regex.Match(textBox1.Text, reg);
45             if (match2.Success)
46             {
47                 MessageBox.Show("Do not send");
48                 return;
49             }
50             else
51             {
52                 MessageBox.Show("Send success");
53                 return;
54             }
55 
56         }



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Esther at November 17, 2013 - 11:44 AM