C#使用正则表达式实现常见的格式验证
正则表达式在程序设计中有着重要的位置,经常被用于处理字符串信息。
用Regex类的IsMatch方法,使用正则表达式可以验证电话号码是否合法。
一、涉及到的知识点
Regex类的IsMatch方法用于指示正则表达式使用pattern参数中指定的正则表达式是否在输入字符串中找到匹配项。语法格式如下:
public static bool IsMatch(string input,string patterm)
参数说明
Input:字符串对象,表示要搜索匹配项的字符串。
Pattern:字符串对象,表示要匹配的正则表达式模式。
Bool:返回布尔值,如果正则表达式找到匹配项,则返回值为true,否则返回值为false。
其中,正则表达式中匹配位置的元字符“^”。正则表达式中“^”用于匹配行首,如果正则表达式匹配以First开头的行,则正则表达式如下:^First。
如果电话号码的格式:xxx-xxxxxxxx,其中,x—代表数字,那么匹配的正则表达式是:^(\d{3,4}-)?\d{6,8}$。
如果密码有a-z、A-Z、0-9组成,并且至少一个大小写字母和数字,那么其正则表达式:[A-Za-z]+[0-9];
如果密码有a-z、A-Z、0-9组成,并且至少一个大小写字母或数字,那么其正则表达式:[A-Za-z0-9]+,其中+有没有都可以;
如果把正则表达式改为[A-Z]+[a-z]+[0-9],就变成依次至少一个大写、一个小写、一个数字了,打乱了顺序都不行。
由6位数字组成的邮编的正则表达式:^\d{6}$;
二、实例1:验证电话号码的格式
//使用正则表达式验证电话号码
namespace _070
{
public partial class Form1 : Form
{
private Label? label1;
private Label? label2;
private Label? label3;
private Button? button1;
private TextBox? textBox1;
public Form1()
{
InitializeComponent();
Load += Form1_Load;
}
private void Form1_Load(object? sender, EventArgs e)
{
//
// label1
//
label1 = new Label
{
AutoSize = true,
Location = new Point(36, 22),
Name = "label1",
Size = new Size(68, 17),
TabIndex = 0,
Text = "输入号码:"
};
//
// label2
//
label2 = new Label
{
AutoSize = true,
Location = new Point(156, 49),
Name = "label2",
Size = new Size(79, 17),
TabIndex = 1,
Text = "xxx-xxxxxxxx"
};
//
// label3
//
label3 = new Label
{
AutoSize = true,
Location = new Point(36, 49),
Name = "label3",
Size = new Size(68, 17),
TabIndex = 2,
Text = "号码格式:"
};
//
// button1
//
button1 = new Button
{
Location = new Point(160, 76),
Name = "button1",
Size = new Size(75, 23),
TabIndex = 3,
Text = "号码验证",
UseVisualStyleBackColor = true
};
button1.Click += Button1_Click;
//
// textBox1
//
textBox1 = new TextBox
{
Location = new Point(115, 16),
Name = "textBox1",
Size = new Size(120, 23),
TabIndex = 4
};
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(294, 111);
Controls.Add(textBox1);
Controls.Add(button1);
Controls.Add(label3);
Controls.Add(label2);
Controls.Add(label1);
Name = "Form1";
StartPosition = FormStartPosition.CenterScreen;
Text = "使用正则表达式验证电话号码";
}
/// <summary>
/// 验证电话号码格式是否正确
/// </summary>
private void Button1_Click(object? sender, EventArgs e)
{
if (!IsTelephone(textBox1!.Text))
{
MessageBox.Show("电话号码格式不正确");
}
else
{
MessageBox.Show("电话号码格式正确");
}
}
/// <summary>
/// 验证电话号码格式是否匹配
/// </summary>
/// <param name="str_telephone">电话号码信息</param>
/// <returns>方法返回布尔值</returns>
public static bool IsTelephone(string str_telephone)
{
return MyRegex().IsMatch(str_telephone);
}
[System.Text.RegularExpressions.GeneratedRegex(@"^(\d{3,4}-)?\d{6,8}$")]
private static partial System.Text.RegularExpressions.Regex MyRegex();
}
}
三、实例2:验证密码的格式
// 使用正则表达式验证密码格式
namespace _071
{
public partial class Form1 : Form
{
private GroupBox? groupBox1;
private Button? button1;
private TextBox? textBox1;
private Label? label1;
public Form1()
{
InitializeComponent();
Load += Form1_Load;
}
private void Form1_Load(object? sender, EventArgs e)
{
//
// button1
//
button1 = new Button
{
Location = new Point(171, 58),
Name = "button1",
Size = new Size(100, 23),
TabIndex = 2,
Text = "验证密码格式",
UseVisualStyleBackColor = true
};
button1.Click += Button1_Click;
//
// textBox1
//
textBox1 = new TextBox
{
Location = new Point(126, 24),
Name = "textBox1",
Size = new Size(145, 23),
TabIndex = 1
};
//
// label1
//
label1 = new Label
{
AutoSize = true,
Location = new Point(35, 30),
Name = "label1",
Size = new Size(68, 17),
TabIndex = 0,
Text = "输入密码:"
};
//
// groupBox1
//
groupBox1 = new GroupBox
{
Location = new Point(12, 12),
Name = "groupBox1",
Size = new Size(307, 87),
TabIndex = 0,
TabStop = false,
Text = "密码必须由数字和大小写字母组成"
};
groupBox1.Controls.Add(button1);
groupBox1.Controls.Add(textBox1);
groupBox1.Controls.Add(label1);
groupBox1.SuspendLayout();
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(331, 111);
Controls.Add(groupBox1);
Name = "Form1";
StartPosition = FormStartPosition.CenterScreen;
Text = "正则表达式验证密码格式";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
}
private void Button1_Click(object? sender, EventArgs e)
{
if (!IsPassword(textBox1!.Text.Trim()))
{
MessageBox.Show("密码格式不正确!!!");
}
else
{
MessageBox.Show("密码格式正确!!!!!");
}
}
/// <summary>
/// 验证码码输入条件
/// </summary>
/// <param name="str_password">密码字符串</param>
/// <returns>返回布尔值</returns>
public static bool IsPassword(string str_password)
{
return MyRegex().IsMatch(str_password);
}
[System.Text.RegularExpressions.GeneratedRegex(@"[A-Za-z]+[0-9]")]//至少有一个字母,至少有一个数字
//[System.Text.RegularExpressions.GeneratedRegex(@"[A-Z]+[a-z]+[0-9]")]//依次至少有一个大写一个小写一个
//[System.Text.RegularExpressions.GeneratedRegex(@"[A-Za-z0-9]+")]//至少一个
private static partial System.Text.RegularExpressions.Regex MyRegex();
}
}
四、实例3:验证邮编的格式
// 用正则表达式验证邮编合法性
namespace _072
{
public partial class Form1 : Form
{
private GroupBox? groupBox1;
private TextBox? textBox1;
private Button? button1;
private Label? label1;
public Form1()
{
InitializeComponent();
Load += Form1_Load;
}
private void Form1_Load(object? sender, EventArgs e)
{
//
// textBox1
//
textBox1 = new TextBox
{
Location = new Point(139, 32),
Name = "textBox1",
Size = new Size(100, 23),
TabIndex = 2
};
//
// button1
//
button1 = new Button
{
Location = new Point(139, 61),
Name = "button1",
Size = new Size(100, 23),
TabIndex = 1,
Text = "验证邮编",
UseVisualStyleBackColor = true
};
button1.Click += Button1_Click;
//
// label1
//
label1 = new Label
{
AutoSize = true,
Location = new Point(55, 35),
Name = "label1",
Size = new Size(68, 17),
TabIndex = 0,
Text = "输入邮编:"
};
//
// groupBox1
//
groupBox1 = new GroupBox
{
Location = new Point(12, 12),
Name = "groupBox1",
Size = new Size(280, 98),
TabIndex = 0,
TabStop = false,
Text = "验证邮编格式:"
};
groupBox1.Controls.Add(textBox1);
groupBox1.Controls.Add(button1);
groupBox1.Controls.Add(label1);
groupBox1.SuspendLayout();
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(304, 122);
Controls.Add(groupBox1);
Name = "Form1";
StartPosition = FormStartPosition.CenterScreen;
Text = "验证邮编格式合法性";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
}
private void Button1_Click(object? sender, EventArgs e)
{
if (!IsPostalcode(textBox1!.Text))
{
MessageBox.Show("邮政编号不正确!!!");
}
else
{
MessageBox.Show("邮政编号正确!!!!!");
}
}
/// <summary>
/// 验证邮编格式是否正确
/// </summary>
/// <param name="str_postalcode">邮编字符串</param>
/// <returns>返回布尔值</returns>
public static bool IsPostalcode(string str_postalcode)
{
return MyRegex().IsMatch(str_postalcode);
}
[System.Text.RegularExpressions.GeneratedRegex(@"^\d{6}$")]
private static partial System.Text.RegularExpressions.Regex MyRegex();
}
}
到此这篇关于C#使用正则表达式实现常见的格式验证的文章就介绍到这了,更多相关C#格式验证内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
使用VS2010 C#开发ActiveX控件(下),完整代码打包下载
我们介绍了开发、打包、发布、使用ActiveX控件的全过程。在演示程序中,我们没有调用串口通信和读卡器Dll程序,由于我们读卡器的原始Dll是使用其它语言进行开发的,对C#来说,是非托管代码,因此我们还需要在代码级别进行非托管代码的安全性设置2011-05-05


最新评论