正则表达式的验证程序
2006-09-07 17:16
295 查看
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();
}
}
{
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();
}
}
相关文章推荐
- 正则表达式验证正确性测试程序
- 微信小程序实现简单input正则表达式验证功能示例
- django 注册、登录及第三方接口程序(3):验证之正则表达式
- [VB.NET]谁能帮我用验证国内电话号码的正则表达式写一个验证程序啊?
- 【微信小程序教程】——input正则表达式验证的使用
- 过滤全文验证正则表达式的一个小程序
- Java - 华为机试训练 - 密码验证合格程序(正则表达式判断)
- 验证身份证号是否合法的小程序(正则表达式)
- JavaScript 正则表达式 验证网址
- JS正则表达式验证账号、手机号、电话和邮箱
- JS 常用正则验证表达式
- 密码验证,密码强度正则表达式
- 几个常用的正则验证表达式-可以满足一般需求
- 密码验证的正则表达式 (6-16位字母和数字组合)
- 一些我经过验证的-有效的-【正则表达式】-JAVA
- JS正则表达式验证数字
- 用javascript验证表单数据正则表达式汇总
- 常用正则表达式大全,手机、电话、邮箱、身份证(最严格的验证)、IP地址、网址、日期等,一般前台js验证,来这里就够了...
- 用正则表达式验证用户名
- validate自定义验证(正则表达式+两个input框验证)