首页
/
每日頭條
/
科技
/
c語言正則表達式怎麼寫
c語言正則表達式怎麼寫
更新时间:2026-06-03 15:11:42
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-06-03
冬季珊瑚絨睡衣可愛
冬季珊瑚絨睡衣可愛
撸貓已經成為了年輕人最新的解壓方式軟綿綿、毛茸茸、暖呼呼的手感撸一次就會非常上瘾天再冷都覺得暖和無比今天小編推薦的是能鋪在床上的“毛茸茸”—法蘭絨床上四件套。推薦理由:1、床單被褥的“毛皮大衣”,比普通4件套平均升溫2℃,過冬就要有毛才溫暖...
2026-06-03
手機檢測第三方屏幕是什麼意思
手機檢測第三方屏幕是什麼意思
我們在選購手機的時候,常常會看到屏幕通過了“SGS護眼認證、VDE顯示效果認證”之類的名詞,在日常使用手機的時候,這些在“認證”光環下的屏幕,真的能實現所謂的護眼嗎?屏幕在運行的過程中,會散發不同波長的藍光,其中波長400~450nm之間的...
2026-06-03
手機屏幕有點黃是什麼原因
手機屏幕有點黃是什麼原因
多數用戶在使用智能手機的過程中,突然發現屏幕顯示顔色偏黃,便認為屏幕質量出現了問題。其實不然,手機屏幕顯示顔色偏黃,多數情況下是因為打開了“護眼模式”這項功能,而并非質量問題。手機屏幕顔色為什麼會偏黃智能手機的屏幕在顯示過程中會散發出藍光,...
2026-06-03
坦克世界1.5版本新坦克
坦克世界1.5版本新坦克
在夏去秋來的這段時間,《坦克世界》的玩家也參與了不少精彩的活動。國服周年慶典活動讓玩家獲得了豐厚獎勵,如今活動已接近尾聲,不過玩家無需失落,因為《坦克世界》近期最重大的更新版本1.18終于到來。想了解更為詳細的版本内容,下面讓我們一起來看看...
2026-06-03
Copyright 2023-2026 - www.tftnews.com All Rights Reserved