首页
/
每日頭條
/
科技
/
c語言正則表達式怎麼寫
c語言正則表達式怎麼寫
更新时间:2026-04-19 16:52:36
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
推荐阅读
ubuntu安裝ssh方法
ubuntu安裝ssh方法
ubuntu安裝ssh方法?用Ctrl+Alt+T打開Ubuntu終端,輸入sudoapt-getupdate命令–>回車–>輸入當前登錄用戶的管理員密碼–>回車以實現更新Ubuntu系統,今天小編就來說說關于ubuntu安裝ssh方法?下...
2026-04-19
肖戰最近活動怎麼樣
肖戰最近活動怎麼樣
6月22日,肖戰正式官宣成為某咖啡品牌代言人,這已經是2021年以來他所新增的第15個代言了,毫無意外,又是線上線下各種同款紛紛秒空售罄的時刻。這已經是肖戰代言官宣後的常态,有媒體還将其命名為“肖戰效應”。而這種場景,在去年幾乎無法想象。這...
2026-04-19
英特爾x99h主闆屬于什麼檔次
英特爾x99h主闆屬于什麼檔次
粉絲要求,必須MATX規格主闆,處理器是英特爾2278GES,八核心十六線程酷睿I9級别,500元,能搞定嗎?粉絲難,魔改君更難!看着貌似全新,擋闆沒有拆封,但顯然不是挖礦退役,因為PCI-E接口數量不夠真的是全新沒有拆封的擋闆接口也很新W...
2026-04-19
手機分期付款的利與弊
手機分期付款的利與弊
買手機分期付款怎麼樣,這是很多學生族或工薪族比較關心的一個問題,目前一台好一點的手機動不動都要幾千塊錢,分期付款買手機确實能減輕不少經濟負擔,那麼怎麼在網上分期付款買手機呢?買手機分期付款利弊關于在網上分期付款買手機,我們首先要了解這其中的...
2026-04-19
手機上怎麼玩電腦遊戲
手機上怎麼玩電腦遊戲
手機上怎麼玩電腦遊戲?怎麼在電腦上玩手機遊戲?手機遊戲現在很火爆,大家随時随地都可以拿出手機玩上一會,不過很多時候需要長時間挂機的話還是想要用電腦來玩遊戲遊戲耗電量比較大,如果不停的充電放電,手機電池的壽命會大幅度削減,為了我們的愛機,還是...
2026-04-19
Copyright 2023-2026 - www.tftnews.com All Rights Reserved