首页
/
每日頭條
/
科技
/
c語言正則表達式怎麼寫
c語言正則表達式怎麼寫
更新时间:2026-07-13 05:19:19
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
推荐阅读
max232是什麼芯片
max232是什麼芯片
演示機型:華為MateBookX系統版本:win10MAX232芯片是美信(MAXIM)公司專為RS-232标準串口設計的單電源電平轉換芯片,使用+5v單電源供電。MAX220–MAX249系列線驅動器/接收器,專為EIA/TIA-232E以及V.28/V.24通信接口設計,尤其是無法提供±12V電源的應用。這些器件特别适合電池供電系統,這是由于其低功耗關斷模式可以将功耗
2026-07-13
和平精英界面設置
和平精英界面設置
1、我們先進入到和平精英遊戲界面2、接下來我們點擊右下角圖示的設置按鈕選項3、然後在設置選項中選擇圖...
2026-07-13
三星s8信号差怎麼解決
三星s8信号差怎麼解決
1、可能是您所處的位置信号覆蓋不佳,建議您重啟手機或更換其他位置嘗試。2、手機連接無線網絡将設備更新至最新版本。注:升級前請備份設備中數據(如聯系人、信息、圖片等)。3、若無效,請關機取出SIM卡重新插入嘗試。4、更換其他SIM卡。5、若信号制依然zhidao不穩定,請備份手機中數據(聯系人、信息等),恢複出廠設置嘗試。
2026-07-13
電腦重裝系統删除的東西怎麼找回
電腦重裝系統删除的東西怎麼找回
1、将U盤制作成UU盤啟動盤,然後重啟電腦,當顯示器中出現品牌LOGO标志的時候U盤啟動快捷鍵,系統...
2026-07-13
打印機連在電腦上怎麼用
打印機連在電腦上怎麼用
1、打開電腦,在搜索欄搜索“控制面闆”。2、點擊進入後,選擇“硬件和聲音”。3、選擇“設備和打印機”...
2026-07-13
Copyright 2023-2026 - www.tftnews.com All Rights Reserved