首页
/
每日頭條
/
科技
/
c語言正則表達式怎麼寫
c語言正則表達式怎麼寫
更新时间:2026-04-08 22:02:24
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
推荐阅读
windows更新怎麼回到之前的版本
windows更新怎麼回到之前的版本
我們更新系統之後,難免會出現各種問題,例如系統補丁導緻的藍屏、軟件不兼容,但是在沒有更新之前電腦是沒有任何問題的,那麼Win10升級後如何恢複原來的版本呢?下面小編給大家分享一下Win10系統退回升級前的版本方法。工具/原料系統版本:win...
2026-04-08
心形壁紙高清手機壁紙
心形壁紙高清手機壁紙
少女心卡通圖案手機壁紙高清壁紙無水印,
2026-04-08
怎麼解決手機耗電很快的問題
怎麼解決手機耗電很快的問題
說起手機相信大家都不陌生吧,手機已經是我們生活中必不可少的工具了,多年前的手機隻能夠撥打電話,還有一些簡單的多媒體功能。而現在的手機,早已脫離了這個定位,随着手機的不斷升級,各大應用功能運行而生,人們使用手機的頻率越來越高。不僅可以用于生活...
2026-04-08
變頻器常用幾個參數
變頻器常用幾個參數
不同品牌的變頻器,在功能參數的組成内容上基本上是一緻的,功能參數的内容組成我們稱之為功能參數格式。一個功能參數由下面五個部分組成。如下表:下面我們分别對其各部分進行說明參數編号:又叫功能碼,編碼,功能等。參數編号是變頻器功能的代号,編号的目...
2026-04-08
柯偉力回家工作室
柯偉力回家工作室
來源:台州公安男子雙手攥緊手心早已滲出汗水屏幕那頭一位滿臉滄桑的婦女也抿着嘴巴緊盯男子她身後站着幾名家屬就等柯偉力說出那句話……小小的視頻聊天室,成了連接屏幕兩頭的橋梁,承載了線上認親的重托。來自江蘇的王峰(化名)和來自新疆的張梅(化名),...
2026-04-08
Copyright 2023-2026 - www.tftnews.com All Rights Reserved