C#使用正則表達式實現(xiàn)常見的格式驗證
正則表達式在程序設計中有著重要的位置,經常被用于處理字符串信息。
用Regex類的IsMatch方法,使用正則表達式可以驗證電話號碼是否合法。
一、涉及到的知識點
Regex類的IsMatch方法用于指示正則表達式使用pattern參數(shù)中指定的正則表達式是否在輸入字符串中找到匹配項。語法格式如下:
public static bool IsMatch(string input,string patterm)
參數(shù)說明
Input:字符串對象,表示要搜索匹配項的字符串。
Pattern:字符串對象,表示要匹配的正則表達式模式。
Bool:返回布爾值,如果正則表達式找到匹配項,則返回值為true,否則返回值為false。
其中,正則表達式中匹配位置的元字符“^”。正則表達式中“^”用于匹配行首,如果正則表達式匹配以First開頭的行,則正則表達式如下:^First。
如果電話號碼的格式:xxx-xxxxxxxx,其中,x—代表數(shù)字,那么匹配的正則表達式是:^(\d{3,4}-)?\d{6,8}$。
如果密碼有a-z、A-Z、0-9組成,并且至少一個大小寫字母和數(shù)字,那么其正則表達式:[A-Za-z]+[0-9];
如果密碼有a-z、A-Z、0-9組成,并且至少一個大小寫字母或數(shù)字,那么其正則表達式:[A-Za-z0-9]+,其中+有沒有都可以;
如果把正則表達式改為[A-Z]+[a-z]+[0-9],就變成依次至少一個大寫、一個小寫、一個數(shù)字了,打亂了順序都不行。
由6位數(shù)字組成的郵編的正則表達式:^\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 = "密碼必須由數(shù)字和大小寫字母組成" }; 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]")]//至少有一個字母,至少有一個數(shù)字 //[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#使用正則表達式實現(xiàn)常見的格式驗證的文章就介紹到這了,更多相關C#格式驗證內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用VS2010 C#開發(fā)ActiveX控件(下),完整代碼打包下載
我們介紹了開發(fā)、打包、發(fā)布、使用ActiveX控件的全過程。在演示程序中,我們沒有調用串口通信和讀卡器Dll程序,由于我們讀卡器的原始Dll是使用其它語言進行開發(fā)的,對C#來說,是非托管代碼,因此我們還需要在代碼級別進行非托管代碼的安全性設置2011-05-05C#實現(xiàn)將一個矩陣分解為對稱矩陣與反稱矩陣之和的方法
這篇文章主要介紹了C#實現(xiàn)將一個矩陣分解為對稱矩陣與反稱矩陣之和的方法,較為詳細的分析了矩陣分解運算的原理與C#實現(xiàn)技巧,需要的朋友可以參考下2015-08-08解析C#中用Process類終止進程,執(zhí)行命令的深入分析
本篇文章是對C#中用Process類終止進程,執(zhí)行命令進行了詳細的分析介紹,需要的朋友參考下2013-05-05