首页
/
每日頭條
/
科技
/
c語言正則表達式怎麼寫
c語言正則表達式怎麼寫
更新时间:2026-05-12 13:33:02
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
推荐阅读
蘋果13promax的性能咋樣
蘋果13promax的性能咋樣
如今手機的性能對于我們而言已經溢出了,那麼搭載地表最強手機處理器的A15,能否與價格高達25000元的PC電腦比拼一下速度呢?從配置表單可以看到,iPhone13ProMax搭載A15處理器,6GB内存,5核心GPU;而售價高達25000元...
2026-05-12
軌道交通大專類就業前景
軌道交通大專類就業前景
高考資訊·指導雜志大學、專業、學科、志願、勵志……那些事兒關注如果說哪項現代化交通工具帶給人們的生活影響很大,高鐵是其中之一。作為高鐵的神經中樞,列車運行控制系統在保證列車運行安全和提升列車運行效率方面扮演着舉足輕重的角色,而這一切又和軌道...
2026-05-12
山東科技大學泰山校區位置
山東科技大學泰山校區位置
7日上午,齊魯晚報·齊魯壹點記者來到位于泰安市岱嶽區山口鎮的山東科技大學泰山科技學院,進入學校,視野十分開闊,遙望泰山,風景秀麗,西臨濟泰高速,北依環山路,交通也很便利。校園建築結合山東地方文化特色和古代書院圍合式格局,風格獨特。學校設置了...
2026-05-12
電力工業級以太網交換機自主研發
電力工業級以太網交換機自主研發
現如今,随着中國制造2025和工業4.0的逐漸展開,工業交換機的市場越來越大,尤其是在電力,交通,及工業自動化行業,但是進口品牌的在國内的份額卻在不斷的萎縮,這就給國内的很多工業交換機廠家一個非常大的機會。目前,就國内而言,工業交換機品牌主...
2026-05-12
ps人像橡皮擦
ps人像橡皮擦
魔棒工具是一個十分簡單的選區工具之一,魔棒對背景簡單的單色背景摳圖做選區十分好用,大家來看看這個摳圖工具的使用吧。本章使用到的快捷鍵:CtrlO打開文件CtrlJ複制圖層(高手都這麼做)CTRLSHIFTI反選1、這種純色背景的摳圖,是使用...
2026-05-12
Copyright 2023-2026 - www.tftnews.com All Rights Reserved