首页
/
每日頭條
/
科技
/
c語言正則表達式怎麼寫
c語言正則表達式怎麼寫
更新时间:2026-05-22 10:37:04
C#編程正則表達式驗證數字和字母

c語言正則表達式怎麼寫(源代碼C編程正則表達式驗證數字和字母)1

程序界面

源代碼:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Text.RegularExpressions; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string str = textBox1.Text; if(isyz1(str)) { MessageBox.Show("輸入正确","提示"); } else { MessageBox.Show("輸入有誤,請重新輸入", "提示"); } } private void button3_Click(object sender, EventArgs e) { string str = textBox3.Text; if (isyz3(str)) { MessageBox.Show("輸入正确", "提示"); } else { MessageBox.Show("輸入有誤,請重新輸入", "提示"); } } private void button2_Click(object sender, EventArgs e) { string str = textBox2.Text; if (isyz2(str)) { MessageBox.Show("輸入正确", "提示"); } else { MessageBox.Show("輸入有誤,請重新輸入", "提示"); } } private void button4_Click(object sender, EventArgs e) { string str = textBox4.Text; if (isyz4(str)) { MessageBox.Show("輸入正确", "提示"); } else { MessageBox.Show("輸入有誤,請重新輸入", "提示"); } } private void button5_Click(object sender, EventArgs e) { string str = textBox5.Text; if (isyz5(str)) { MessageBox.Show("輸入正确", "提示"); } else { MessageBox.Show("輸入有誤,請重新輸入", "提示"); } } //驗證非0正整數 public bool isyz1(string sz) { return Regex.IsMatch(sz, @"^\ ?[1-9][0-9]*$"); } //驗證非0負整數 public bool isyz3(string sz) { return Regex.IsMatch(sz, @"^\-[1-9][0-9]*$"); } //驗證大寫字母 public bool isyz2(string sz) { return Regex.IsMatch(sz, @"^[A-Z] $"); } //驗證小寫字母 public bool isyz4(string sz) { return Regex.IsMatch(sz, @"^[a-z] $"); } public bool isyz5(string sz) { return Regex.IsMatch(sz, @"^[A-Za-z] $"); } } }

結語:

熟練掌握正則表達式數字和字母的驗證。

喜歡的請關注、收藏!

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
手機設置路由器步驟圖
手機設置路由器步驟圖
手機設置路由器步驟圖?家有電腦的讀者都知道路由器的用途,可以讓家裡的寬帶進行無線上網,這樣就不用受網線的羁絆,在家裡就能實現移動上網,并且電腦和手機平闆等設備都能共享一個網絡,是不是很爽很開心?,我來為大家講解一下關于手機設置路由器步驟圖?...
2026-05-22
電腦免費大型3d遊戲
電腦免費大型3d遊戲
日常生活中有時候需要存儲照片、視頻或者遊戲等文件,對于較小的文件用網絡傳輸确實比較快,但是對于較大的文件來說,拷貝還是要稍微快一點。對于遊戲愛好者來說,直接使用硬盤讀取要稍微好點,畢竟大部分稍微好玩的大型3D遊戲都要30GB起步。最近入手了...
2026-05-22
oppo手機的隐藏功能
oppo手機的隐藏功能
oppo手機的隐藏功能?音量鍵的使用音量加鍵和開機鍵長按三秒就可以截圖,或者音量減鍵和開機鍵三秒同時按也可以截圖,相信這個是很多使用OPPO手機的人都知道在手機意外死機的情況下,隻需要長按音量加鍵十秒就會重啟手機平時在遇到一些比較有趣的瞬間...
2026-05-22
微博視頻怎麼保存
微博視頻怎麼保存
微博視頻怎麼保存?小米10手機、MIUI12.0.1系統、微博10.8.3版本,我來為大家科普一下關于微博視頻怎麼保存?下面希望有你要的答案,我們一起來看看吧!微博視頻怎麼保存小米10手機、MIUI12.0.1系統、微博10.8.3版本。首...
2026-05-22
硬盤壞了不讀盤怎麼修複
硬盤壞了不讀盤怎麼修複
硬盤這上圖這樣無法打開,很多人都會丢了,今天教大家用Windows系統自帶工具修複硬盤,希望對大家有用第一步:首先按下鍵盤上的按鈕winR鍵調出運行對話框鍵盤win+R然後在打開的運行對話框中輸入CMD回車。第二步:在打開的命令行輸入chk...
2026-05-22
Copyright 2023-2026 - www.tftnews.com All Rights Reserved