首页
/
每日頭條
/
科技
/
c語言正則表達式怎麼寫
c語言正則表達式怎麼寫
更新时间:2026-04-18 04:18:37
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
推荐阅读
奧迪q7一箱油可以跑多少公裡
奧迪q7一箱油可以跑多少公裡
1、07款奧迪Q7一箱油,一般來說車子綜合路況條件下能夠跑500公裡左右,車子全程高速行駛大體能夠跑700-800公裡左右。2、奧迪Q7是一款強調舒适性的中大型SUV,将運動性、功能性、高科技和豪華品質巧妙地融為一體。在公路上,它憑借無限動感的操控性與運動特性脫穎而出。3、奧迪Q7在越野道路面,它強勁的驅動力更令人驚歎。毋庸置疑,奧迪Q7是奧迪公司--quattro技術創始者的又一驚世傑作。4、奧
2026-04-18
電腦一直自動重啟怎麼回事
電腦一直自動重啟怎麼回事
1、溫度過高。如果計算機本身散熱出現問題的話,那麼當我們長時間使用的話,Ghostwin7系統就會容...
2026-04-18
vue中data為什麼必須是函數
vue中data為什麼必須是函數
演示機型:華為MateBookX系統版本:win10APP版本:vue2.9.6vue中data必須是函數是為了保證組件的獨立性和可複用性,data是一個函數,組件實例化的時候這個函數将會被調用,返回一個對象,計算機會給這個對象分配一個内存地址,你實例化幾次,就分配幾個内存地址,他們的地址都不一樣,所以每個組件中的數據不會相互幹擾,改變其中一個組件的狀态,其它組件不變。Vue:Vue.js是一個用
2026-04-18
攝影入門10大技巧
攝影入門10大技巧
1、鏡頭是一個相機的關鍵,很多朋友都會問我買什麼鏡頭比較好,很多朋友會比較喜歡變焦鏡頭,因為變焦鏡頭...
2026-04-18
光刻機為什麼那麼難制造
光刻機為什麼那麼難制造
1、首先,光刻機的透鏡,光源技術難度大,研發要求曆史和時間比較長。其實光刻機的發展曆史非常悠久,從9...
2026-04-18
Copyright 2023-2026 - www.tftnews.com All Rights Reserved