C#正则表达式
//后台.CS文件验证Email格式是否正确: public bool IsEmail(string str_Email) { return System.Text.RegularExpressions.Regex.IsMatch(str_Email, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); } 1. 验证 E-mail格式 public bool IsEmail(string str_Email) { return System.Text.RegularExpressions.Regex.IsMatch(str_Email,@"^([/w-/.]+)@((/[[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.)|(([/w-]+/.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(/]?)$"); } 2. 验证 IP 地址 public bool IPCheck(string IP) { string num = "(25[0-5]|2[0-4]//d|[0-1]//d{2}|[1-9]?//d)"; return Regex.IsMatch(IP,("^" + num + "//." + num + "//." + num + "//." + num + "$")); } 3. 验证 URL public bool IsUrl(string str_url) { return System.Text.RegularExpressions.Regex.IsMatch(str_url, @"http(s)?://([/w-]+/.)+[/w-]+(/[/w- ./?%&=]*)?"); } 二. 常用数字验证技巧 1. 验证电话号码 public bool IsTelephone(string str_telephone) { return System.Text.RegularExpressions.Regex.IsMatch(str_telephone, @"^(/d{3,4}-)?/d{6,8}$"); } 2. 输入密码条件(字符与数据同时出现) public bool IsPassword(string str_password) { return System.Text.RegularExpressions.Regex.IsMatch(str_password, @"[A-Za-z]+[0-9]"); } 3. 邮政编号 public bool IsPostalcode(string str_postalcode) { return System.Text.RegularExpressions.Regex.IsMatch(str_postalcode, @"^/d{6}$"); } 4. 手机号码 public bool IsHandset(string str_handset) { return System.Text.RegularExpressions.Regex.IsMatch(str_handset, @"^[1]+[3,5]+/d{9}$"); } 5. 身份证号 public bool IsIDcard(string str_idcard) { return System.Text.RegularExpressions.Regex.IsMatch(str_idcard, @"(^/d{18}$)|(^/d{15}$)"); } 6. 两位小数 public bool IsDecimal(string str_decimal) { return System.Text.RegularExpressions.Regex.IsMatch(str_decimal, @"^[0-9]+(.[0-9]{2})?$"); } 7. 一年的12个月 public bool IsMonth(string str_Month) { return System.Text.RegularExpressions.Regex.IsMatch(str_Month, @"^(0?[[1-9]|1[0-2])$"); } 8. 一个月的31天 public bool IsDay(string str_day) { return System.Text.RegularExpressions.Regex.IsMatch(str_day, @"^((0?[1-9])|((1|2)[0-9])|30|31)$"); } 9. 数字输入 public bool IsNumber(string str_number) { return System.Text.RegularExpressions.Regex.IsMatch(str_number, @"^[0-9]*$"); } 10. 密码长度 (6-18位) public bool IsPasswLength(string str_Length) { return System.Text.RegularExpressions.Regex.IsMatch(str_Length, @"^/d{6,18}$"); } 11. 非零的正整数 public bool IsIntNumber(string str_intNumber) { return System.Text.RegularExpressions.Regex.IsMatch(str_intNumber, @"^/+?[1-9][0-9]*$"); } 三. 常用字符验证技巧 1. 大写字母 public bool IsUpChar(string str_UpChar) { return System.Text.RegularExpressions.Regex.IsMatch(str_UpChar, @"^[A-Z]+$"); } 2. 小写字母 public bool IsLowChar(string str_UpChar) { return System.Text.RegularExpressions.Regex.IsMatch(str_UpChar, @"^[a-z]+$"); } 3. 检查字符串重复出现的词 private void btnWord_Click(object sender, EventArgs e) { System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(label1.Text, @"/b(?<word>/w+)/s+(/k<word>)/b",System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase); if (matches.Count != 0) { foreach (System.Text.RegularExpressions.Match match in matches) { string word = match.Groups["word"].Value; MessageBox.Show(word.ToString(),"英文单词"); } } else { MessageBox.Show("没有重复的单词"); } } 4. 替换字符串 private void button1_Click(object sender, EventArgs e) { string strResult = System.Text.RegularExpressions.Regex.Replace(textBox1.Text, @"[A-Za-z]/*?", textBox2.Text); MessageBox.Show("替换前字符:" + "/n" + textBox1.Text + "/n" + "替换的字符:" + "/n" + textBox2.Text + "/n" + "替换后的字符:" + "/n" + strResult,"替换"); } 5. 拆分字符串 private void button1_Click(object sender, EventArgs e) { //实例: 甲025-8343243乙0755-2228382丙029-32983298389289328932893289丁 foreach (string s in System.Text.RegularExpressions.Regex.Split(textBox1.Text,@"/d{3,4}-/d*")) { textBox2.Text+=s; //依次输出 "甲乙丙丁" } } 6. 验证输入字母 public bool IsLetter(string str_Letter) { return System.Text.RegularExpressions.Regex.IsMatch(str_Letter, @"^[A-Za-z]+$"); } 7. 验证输入汉字 public bool IsChinese(string str_chinese) { return System.Text.RegularExpressions.Regex.IsMatch(str_chinese, @"^[/u4e00-/u9fa5],{0,}$"); } 8. 验证输入字符串 (至少8个字符) public bool IsLength(string str_Length) { return System.Text.RegularExpressions.Regex.IsMatch(str_Length, @"^.{8,}$"); }