由于工作需要,今天看了一天关于正则表达式的相关学习资料,当然都是入门之类的,看完之后,对于正则表达式也算是有了一个大致的了解,简单的正则表达式现在基本可以搞定。这两篇正则式入门资料在本人前两篇文章已经发表了,如果有相同需求的园友,可自行前去观看了解!
下面教大家自己自己动手写自己的正则表达式的验证器。
首先先用VS新建一个新的Windows窗体项目,窗体的布局如下图所示:
附如下后台代码:
View Code
1 private void regex_btn_Click(object sender, EventArgs e)
2 {
3 try
4 {
5 RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
6 Regex test = new Regex(regex_text.Text, selectedRegexOptions);
7 if (test.IsMatch(str_text.Text))
8 {
9 result_text.ForeColor = Color.Red;
10 result_text.Text = "Success!";
11 }
12 else
13 {
14 result_text.ForeColor = Color.Red;
15 result_text.Text = "Failed!";
16 }
17 }
18 catch (Exception ex)
19 {
20 result_text.ForeColor = Color.Red;
21 result_text.Text = "Error+" + ex.Message.ToString();
22 }
23 }
24
25 private RegexOptions GetSelectedRegexOptions()
26 {
27 RegexOptions selectedRegexOptions = RegexOptions.None;
28 if (this.DX_check.Checked == true)
29 {
30 selectedRegexOptions |= RegexOptions.IgnoreCase;
31 }
32 if (mul_check.Checked == true)
33 {
34 selectedRegexOptions = RegexOptions.Multiline;
35 }
36 if (sim_check.Checked == true)
37 {
38 selectedRegexOptions = RegexOptions.Singleline;
39 }
40 if (rl_check.Checked == true)
41 {
42 selectedRegexOptions |= RegexOptions.RightToLeft;
43 }
44 return selectedRegexOptions;
45 }