c语言正则表达式怎么写(源代码C编程正则表达式验证数字和字母)

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] $"); } } }

结语:

熟练掌握正则表达式数字和字母的验证。

喜欢的请关注、收藏!

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页