20131128-正则表达式与委托

[1]网页提取职位信息

namespace 下载职位信息

{

class Program

{

static void Main(string[] args)

{

WebClient wc = new WebClient();

wc.Encoding = Encoding.Default;

string webAddress = "http://localhost:8080/【上海,IT-管理,计算机软件招聘,求职】-前程无忧.htm";

string strRegex = @"<a\s*href=""http://search.51job.com/job/\d{8},c.html"".+>(.+)</a>";

MatchCollection mat = GetWebInfo(wc, webAddress, strRegex);

foreach (Match item in mat)

{

if (item.Success)

{

Console.WriteLine(item.Groups[1].Value);

}

}

Console.WriteLine(mat.Count);

Console.ReadKey();

}

public static MatchCollection GetWebInfo(WebClient wc,string webAddress,string strRegex) {

string html = wc.DownloadString(webAddress);

Regex regex=new Regex(strRegex);

MatchCollection mat = regex.Matches(html);

return mat;

}

}

}

[2]UBB翻译案例

namespace UBB翻译案例

{

class Program

{

static void Main(string[] args)

{

string str = "听说这个论坛是[color=green]小虎[/color]做的.他是[url=http://www.itcast.cn/]传智播客[/url]";

str = Regex.Replace(str, @"\[url=(.+)\](.+)\[/url\]", "<a href=\"$1\">$2</a>");

Console.WriteLine(str);

Console.ReadKey();

}

}

}

[3]敏感词过滤

namespace 敏感词过滤

{

/*mgc.txt格式

成人午夜场={BANNED}

成人小说={BANNED}

出售={MOD}

*/

public partial class Form1 : Form

{

//审核

StringBuilder sbMod = new StringBuilder();

//禁止

StringBuilder sbBanned = new StringBuilder();

public Form1()

{

InitializeComponent();

}

 

private void button1_Click(object sender, EventArgs e)

{

string txt = textBox1.Text;

if (Regex.IsMatch(txt,sbBanned.ToString()))

{

MessageBox.Show("禁止发帖");

}

else if (Regex.IsMatch(txt,sbMod.ToString()))

{

MessageBox.Show("需要审核");

}

else

{

textBox2.Text = txt;

}

}

 

private void Form1_Load(object sender, EventArgs e)

{

//创建存储敏感词的对象

string[] lines = File.ReadAllLines("mgc.txt", Encoding.Default);

for (int i = 0; i < lines.Length; i++)

{

string[] words = lines[i].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);

if (words[1] == "{BANNED}")

{

//拼接一个超级正则表达式

sbMod.Append(words[0] + "|");

}

//拼接一个超级正则表达式

else if (words[1] == "{MOD}")

{

sbBanned.Append(words[0] + "|");

}

}

sbMod.Remove(sbMod.Length - 1, 1);

sbBanned.Remove(sbBanned.Length - 1, 1);

}

}

}

[4]委托计算

namespace 委托计算2

{

class Program

{

//A

static void Main(string[] args)

{

// string [] strs=new string[3];

string[] strs = {"qwe","df","lyui" };

//Step4: 调用BMethod2(),参数包含A类中的Method1

ChangeStr cs = new ChangeStr();

cs.ChangeString(strs, AddString);

for (int i = 0; i < strs.Length; i++)

{

Console.WriteLine(strs[i]);

}

Console.ReadKey();

}

//Step1: A 方法Method1()

private static string AddString(string str)

{

return "====" + str + "===";

}

}

//Step2: A类提供的方法定义为委托

public delegate string MyDelegateChangStr(string str);

//Step3: B Method2(arg*, Delegate mdl);

// 此时A中的方法可被B类带到任意处调用

public class ChangeStr

{

public void ChangeString(string [] strs,MyDelegateChangStr mdl)

{

for (int i = 0; i < strs.Length; i++)

{

strs[i] = mdl(strs[i]);

}

}

}

}

[5]两窗体传值

namespace 窗体传值2

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

 

private void button1_Click(object sender, EventArgs e)

{

//Step4:fm1textBox的值传给fm2,并且将改变本类textBox值得方法作为委托方法传给fm2

Form2 fm2 = new Form2(textBox1.Text, ChuanZhi);

fm2.Show();

}

//Step1: A类给本类textBox赋值的方法

private void ChuanZhi(string txt)

{

textBox1.Text = txt;

}

}

//Step2: A类的赋值的方法定义为委托

public delegate void MyDelegate(string txt);

}

 

namespace 窗体传值2

{

public partial class Form2 : Form

{

//定义一个委托属性存放传递过来的委托

MyDelegate _mdl;

public MyDelegate Mdl

{

get { return _mdl; }

set { _mdl = value; }

}

public Form2()

{

InitializeComponent();

}

//Step3 重写窗体2构造函数,fm1-->fm2完成textBox传值;完成委托传递

public Form2(string txt, MyDelegate mdl)

: this()

{

textBox1.Text = txt;

this.Mdl = mdl;

}

private void button1_Click(object sender, EventArgs e)

{

//Step5: 完成fm1<---fm2传值

this.Mdl(textBox1.Text);

this.Close();

}

}

}

posted @ 2014-01-20 21:35  几维  阅读(237)  评论(0编辑  收藏  举报