1 private void button1_Click(object sender, EventArgs e) 2 { 3 string txt = textBox1.Text; 4 5 string[] filtetxt = File.ReadAllLines("网站过滤词.txt", Encoding.Default); //按系统默认编码读取敏感词文件 6 7 ListlistWord = new List (); 8 9 foreach (string str in filtetxt)10 {11 string[] line = str.Split('=');12 if (line[1]=="{Word}")13 {14 listWord.Add(line[0]);15 } 16 }17 18 string strWord= string.Empty;//初始化为空19 20 StringBuilder sb = new StringBuilder();21 foreach (var item in listWord)22 {23 sb.Append(item+"|");//将敏感词拼接成一个用“|”分割的的字符串24 }25 sb.Remove(sb.Length-1,1);//删掉最后一个“|”26 27 strWord= sb.ToString();//拼接{Word}长的正则表达式28 29 30 bool b = Regex.IsMatch(txt,strWord);31 if (b)32 {33 MessageBox.Show("你输入的文字不符合要求~请重新输入~~~");34 }35 }