首页
/
每日頭條
/
科技
/
c語言正則表達式怎麼寫
c語言正則表達式怎麼寫
更新时间:2026-03-29 16:33:57
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
推荐阅读
eclipse導入别人源碼文件夾
eclipse導入别人源碼文件夾
本圖文教程是由電腦win10,eclipse3.2制作。1、先新建一個project,在package上右擊選擇import,選擇General,再選擇其下的FileSystem,點擊next。2、點擊Browse,找到源碼所在的文件夾,然後隻選擇後綴為java的文件,finish完成。
2026-03-29
藍牙耳機怎麼充電
藍牙耳機怎麼充電
1、選用合适的充電器。一般藍牙耳機都有專用的充電器,如果沒有了專用的充電器,可以找充電接口(有的是細口圓孔的,有的是MiniUSB通用接口)一樣的,并且額定輸出功率一樣的充電器使用。2、充電時要把插頭全部插進去,不要隻插入一多半,這樣時間長了損壞機器。在插拔充電插頭時,要輕一些,否則時間長了會導緻插...
2026-03-29
什麼是伺服電機
什麼是伺服電機
1、伺服電機(servomotor)是指在伺服系統中控制機械元件運轉的發動機,是一種補助馬達間接變速...
2026-03-29
抖音賬号是什麼
抖音賬号是什麼
以華為P40,Android11,抖音v14.4.0為例。1、指抖音号。在手機中打開抖音,在個人中心頁面裡,打開的界面點擊右上角三橫線選項。2、然後在彈出的選項中,打開設置。3、接着在打開的設置頁面中,點擊打開賬号與安全。4、然後在這裡點擊打開賬号管理選項。5、打開的界面即可查看登錄的賬号。抖音是由...
2026-03-29
天文望遠鏡是怎樣看到那麼遠的制作原理
天文望遠鏡是怎樣看到那麼遠的制作原理
1、制作原理:天文望遠鏡上一般有兩隻鏡筒,大的是主鏡,是觀測目标所用的;小的叫尋星鏡,是尋找目标所用...
2026-03-29
Copyright 2023-2026 - www.tftnews.com All Rights Reserved