首页
/
每日頭條
/
科技
/
c語言正則表達式怎麼寫
c語言正則表達式怎麼寫
更新时间:2026-08-09 10:23:51
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
推荐阅读
蘋果手機進水充不進去電怎麼辦
蘋果手機進水充不進去電怎麼辦
蘋果手機進水充不進去電怎麼辦?手機進水後系統會自動斷電,所以充不了電,需要先将手機幹燥後再充電,将手機表面的水分擦幹,然後放到米袋中24小時以上,或者放到通風的地方晾兩天,期間不要進行開機、充電等操作,我來為大家科普一下關于蘋果手機進水充不...
2026-08-09
win7資源管理器老是重啟怎麼辦
win7資源管理器老是重啟怎麼辦
使用windwos7系統電腦過程中,有時會遇到了windows資源管理器總重啟的問題,那麼win7電腦windows資源管理器總重啟怎麼辦呢?就此問題,小編整理了win7系統windows資源管理器總重啟的解決技巧,現分享給有需要的用戶。最...
2026-08-09
scratch 趣味教程
scratch 趣味教程
歡迎回來,前面的課程我們已經熟悉了scratch的基本使用,也了解了流程、變量、運算、算法這些編程裡的基本概念,今天最後一課我們再來玩一玩scratch的其他有趣的功能。本節課分3個部分:1,了解scratch的擴展積木包;2,編寫一段音樂...
2026-08-09
不小心删到桌面時間怎麼辦?
不小心删到桌面時間怎麼辦?
不小心删到桌面時間怎麼辦?可以在桌面空白處長按,可以進入編輯模式,編輯模式裡可以有小部件可以添加,這是一個方法,下面我們就來聊聊關于不小心删到桌面時間怎麼辦?接下來我們就一起去了解一下吧!不小心删到桌面時間怎麼辦可以在桌面空白處長按,可以進...
2026-08-09
防火牆設置可以訪問端口
防火牆設置可以訪問端口
防火牆設置可以訪問端口?有時候由于防火牆引起一些服務不能工作主要現象是在服務器本機上telnet某一個端口是正常的,但是在服務器外面telnet該端口卻是不通的一般情況下可以判斷為防火牆問題那麼我們改怎麼樣去判斷呢,下面壹基比小喻就開給大家...
2026-08-09
Copyright 2023-2026 - www.tftnews.com All Rights Reserved