现在的位置: 首页 > 综合 > 正文

c#与正则表达式

2012年10月24日 ⁄ 综合 ⁄ 共 1753字 ⁄ 字号 评论关闭

    学习c#,进行格式验证时,自己花了很长时间学习了正则表达式,花了点时间,整理了一下,供大家参考一下,有什么错误,希望大神提出来.

  

using Sysyem.Text.RegularExpressions.  //必须引用的命名空间

案例一 :  验证电话号码

       Regex.Ismatch(str_telephone,@"6(\d{3,4}-)?\d{6,8}$");

 

案例二 :  验证输入密码条件

       Regex.Ismatch(str_password,@"[A-Za-z]+[0-9]");

 

 

案例三 :  验证邮政编码       

       Regex.Ismatch(str_postalcode,@"^\d{6}$");

 

 

案例四 :  验证手机号     

   Regex.Ismatch(str_handset,@"^[1][3-5]\d{9}$");

 

 

案例五 :  验证身份证号  

Regex.Ismatch(str_idcard,@"(^\d{18}$|^\d{15}$");

 

 

案例六 :  验证两位小数  

Regex.Ismatch(str_decimal,@"^[0-9]+\.[0-9]{2}$");

 

 

案例七 :  验证一年中的十二月份  

Regex.Ismatch(str_month,@"^(0?[[1-9]|1[0-2]]$");

 

 

案例八 :  验证一个月的三十一天

Regex.Ismatch(str_day,@"^((0?[[1-9]|((1|2)[0-9])|30|31)$");

 

 

案例九 :  验证数字输入

Regex.Ismatch(str_number,@"^[0-9]*$");

 

 

案例十 :  验证密码长度

Regex.Ismatch(str_Length,@"^\d{6-18}$");

 

 

案例十一 :  验证非零正整数

Regex.Ismatch(str_intNumber,@"^\+?[1-9][0-9]*$");

 

 

案例十二 :  验证非零负整数

Regex.Ismatch(str_intNumber,@"^\-?[1-9][0-9]*$");

 

 

案例十三 :  验证大写字母

Regex.Ismatch(str_UpChar,@"^\[A-Z]+$");

 

 

案例十四 :  验证小写字母

Regex.Ismatch(str_UpChar,@"^\[a-z]+$");

 

 

案例十五 :  检查重复出现的词

 

 

 

案例十六 :  替换字符

 

 

 

案例十七 : 拆分字符串

String[] _str=Regex.Split(txtSplit.Text,"[1-9]");

 

案例十八 : 验证输入字符

Regex.IsMatch(str_letter,@"^[A-Za-z]+$");

 

 

案例十九 : 验证中文汉字输入

Regex.IsMatch(str_chinese,@"^[\u4e00-\u9fa5]{1,}$");

 

 

案例二十 : 验证输入字符串

Regex.IsMatch(str_Length,@"^[.{8,}$");

 

 

 

案例二一 : 验证Email格式

       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})(\.[a-zA-Z]{2,4}))$");

 

 

 

案例二二 : 验证Email格式

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

       }

 

 

案例二三 : 验证URL格式

 

Regex.IsMatch(str_url,@"http(s)?//([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?");

 

抱歉!评论已关闭.