正则表达式的验证程序

 public class RegexForm : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox TB_Result;
        private System.Windows.Forms.TextBox TB_Input;
        private System.Windows.Forms.TextBox TB_Pattern;
        private System.Windows.Forms.Button Btn_Regex;
        private System.Windows.Forms.Button Btn_Clear;
        private System.Windows.Forms.GroupBox GB_Options;
        ///   <summary>  
        ///   必需的设计器变量。  
        ///   </summary>  
        private System.ComponentModel.Container components = null;

        public RegexForm()
        {
            //  
            //   Windows   窗体设计器支持所必需的  
            //  
            InitializeComponent();

            //  
            //   TODO:   在   InitializeComponent   调用后添加任何构造函数代码  
            //  
            int i = 0;
            foreach (String regexoptions in Enum.GetNames(typeof(RegexOptions)))
            {
                CheckBox cb = new CheckBox();
                cb.Text = regexoptions;
                if (regexoptions == "IgnoreCase")
                    cb.Checked = true;
                cb.Width = 170;
                cb.Left = (i % 3) * 170 + 8;
                cb.Top = (i / 3) * 20 + 8;
                GB_Options.Controls.Add(cb);
                i++;
            }
        }

        ///   <summary>  
        ///   清理所有正在使用的资源。  
        ///   </summary>  
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region   Windows   窗体设计器生成的代码
        ///   <summary>  
        ///   设计器支持所需的方法   -   不要使用代码编辑器修改  
        ///   此方法的内容。  
        ///   </summary>  
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.Btn_Regex = new System.Windows.Forms.Button();
            this.TB_Input = new System.Windows.Forms.TextBox();
            this.TB_Pattern = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.panel2 = new System.Windows.Forms.Panel();
            this.TB_Result = new System.Windows.Forms.TextBox();
            this.Btn_Clear = new System.Windows.Forms.Button();
            this.GB_Options = new System.Windows.Forms.GroupBox();
            this.panel1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.SuspendLayout();
            //    
            //   panel1  
            //    
            this.panel1.Controls.Add(this.GB_Options);
            this.panel1.Controls.Add(this.Btn_Clear);
            this.panel1.Controls.Add(this.Btn_Regex);
            this.panel1.Controls.Add(this.TB_Input);
            this.panel1.Controls.Add(this.TB_Pattern);
            this.panel1.Controls.Add(this.label2);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(504, 208);
            this.panel1.TabIndex = 0;
            //    
            //   Btn_Regex  
            //    
            this.Btn_Regex.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.Btn_Regex.Location = new System.Drawing.Point(436, 0);
            this.Btn_Regex.Name = "Btn_Regex";
            this.Btn_Regex.TabIndex = 4;
            this.Btn_Regex.Text = "Regex";
            this.Btn_Regex.Click += new System.EventHandler(this.button1_Click);
            //    
            //   TB_Input  
            //    
            this.TB_Input.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.TB_Input.Location = new System.Drawing.Point(80, 32);
            this.TB_Input.Multiline = true;
            this.TB_Input.Name = "TB_Input";
            this.TB_Input.Size = new System.Drawing.Size(412, 68);
            this.TB_Input.TabIndex = 3;
            this.TB_Input.Text = "";
            //    
            //   TB_Pattern  
            //    
            this.TB_Pattern.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.TB_Pattern.Location = new System.Drawing.Point(80, 0);
            this.TB_Pattern.Name = "TB_Pattern";
            this.TB_Pattern.Size = new System.Drawing.Size(348, 21);
            this.TB_Pattern.TabIndex = 2;
            this.TB_Pattern.Text = "";
            //    
            //   label2  
            //    
            this.label2.Location = new System.Drawing.Point(8, 32);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(64, 23);
            this.label2.TabIndex = 1;
            this.label2.Text = "input";
            //    
            //   label1  
            //    
            this.label1.Location = new System.Drawing.Point(8, 8);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(72, 23);
            this.label1.TabIndex = 0;
            this.label1.Text = "Pattern";
            //    
            //   panel2  
            //    
            this.panel2.Controls.Add(this.TB_Result);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel2.Location = new System.Drawing.Point(0, 208);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(504, 133);
            this.panel2.TabIndex = 1;
            //    
            //   TB_Result  
            //    
            this.TB_Result.Dock = System.Windows.Forms.DockStyle.Fill;
            this.TB_Result.Location = new System.Drawing.Point(0, 0);
            this.TB_Result.Multiline = true;
            this.TB_Result.Name = "TB_Result";
            this.TB_Result.Size = new System.Drawing.Size(504, 133);
            this.TB_Result.TabIndex = 0;
            this.TB_Result.Text = "";
            //    
            //   Btn_Clear  
            //    
            this.Btn_Clear.Location = new System.Drawing.Point(8, 64);
            this.Btn_Clear.Name = "Btn_Clear";
            this.Btn_Clear.Size = new System.Drawing.Size(64, 23);
            this.Btn_Clear.TabIndex = 5;
            this.Btn_Clear.Text = "Clear";
            this.Btn_Clear.Click += new System.EventHandler(this.Btn_Clear_Click);
            //    
            //   GB_Options  
            //    
            this.GB_Options.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.GB_Options.Location = new System.Drawing.Point(8, 104);
            this.GB_Options.Name = "GB_Options";
            this.GB_Options.Size = new System.Drawing.Size(484, 96);
            this.GB_Options.TabIndex = 6;
            this.GB_Options.TabStop = false;
            this.GB_Options.Text = "RegexOptions";
            //    
            //   RegexForm  
            //    
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(504, 341);
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.panel1);
            this.Name = "RegexForm";
            this.Text = "RegexForm";
            this.panel1.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        ///   <summary>  
        ///   应用程序的主入口点。  
        ///   </summary>  
        [STAThread]
        static void Main()
        {
            Application.Run(new RegexForm());
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            try
            {
                String pattern = TB_Pattern.Text;
                String input = TB_Input.Text;
                RegexOptions regexoptions = new RegexOptions();
                foreach (Control control in GB_Options.Controls)
                {
                    CheckBox cb = (CheckBox)control;
                    if (cb.Checked)
                    {
                        regexoptions |= (RegexOptions)Enum.Parse(typeof(RegexOptions), cb.Text, true);
                    }
                }

                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern, regexoptions);
                if (reg.IsMatch(input))
                {
                    MatchCollection mc = reg.Matches(input);
                    foreach (Match m in mc)
                    {
                        TB_Result.AppendText(String.Format("{0}   is   match/r/n", m.Groups[0]));
                        for (int i = 0; i < m.Groups.Count; i++)
                        {
                            TB_Result.AppendText(String.Format("Group[{0}]={1}/r/n", i, m.Groups[i].Value));
                        }

                    }
                }
                else
                {
                    this.TB_Result.AppendText(String.Format("'{0}'   is   not   match   '{1}'/r/n", pattern, input));
                }

            }
            catch (Exception excep)
            {
                TB_Result.AppendText(excep.Message + "/r/n");
            }
        }

        private void Btn_Clear_Click(object sender, System.EventArgs e)
        {
            this.TB_Result.Clear();
        }
    } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值