首页
/
每日頭條
/
科技
/
c語言正則表達式怎麼寫
c語言正則表達式怎麼寫
更新时间:2025-11-27 08:42:15
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
推荐阅读
要如何正确地清理電腦垃圾
要如何正确地清理電腦垃圾
1、輸入%temp%命令法。%temp%是系統的環境變量,對應的是C:DocumentsandSet...
2025-11-27
怎麼和孩子成為朋友
怎麼和孩子成為朋友
第一、尊重孩子,不要逗孩子。如果家長想和孩子成為朋友,在平常的時候一定要尊重孩子,千萬不要總是逗孩子。好多家長為了能從孩子那裡獲得一點惡俗的樂趣,會用各種各樣的方式逗孩子,比如有的家長會和孩子開玩笑說他是從垃圾堆裡撿來的,孩子總會在某個時候知道自己不是從垃圾堆裡撿來的。當孩子明白了這件事情以後就會對逗自己玩的那些人感到厭惡,孩子自己厭惡的人,孩子自然就不會與之成為朋友。第二、和孩子玩的時候把自己當
2025-11-27
手機孔灰塵怎麼清理
手機孔灰塵怎麼清理
1、可以試着将膠帶剪成細長條塞到耳機裡将灰塵黏出來,或者将衛生紙卷成長條塞到耳機孔裡轉兩圈,但是這個...
2025-11-27
變壓器的安全距離
變壓器的安全距離
1、10kv放電距離隻要125mm就安全了。2、考慮其他因素,國家要求,變壓器距離牆應大于800mm...
2025-11-27
嘉寶米粉好不好
嘉寶米粉好不好
第一、嘉寶米粉産用大米為原材料,泡開後有着大米的清香,這種濃郁的香味比較符合寶寶的口味,寶寶很愛吃。第二、嘉寶米粉一般用40度左右的水來泡,很容易泡開,不會糊在一起攪拌不開,嘉寶米粉泡開後比較細膩,口感比較好。第三、嘉寶米粉号稱是含鐵量最高的米粉,6個月以後的寶寶主要是補鐵為主,而嘉寶米粉剛好符合寶寶的要求。第四、嘉寶米粉不含糖精,隻有大米本身帶的甜味,寶寶吃了不怕他挑食。而且嘉寶米粉添加劑少,主
2025-11-27
Copyright 2023-2025 - www.tftnews.com All Rights Reserved