Chinaunix首页 | 论坛 | 博客
  • 博客访问: 154458
  • 博文数量: 51
  • 博客积分: 750
  • 博客等级: 上士
  • 技术积分: 440
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-26 17:27
文章分类

全部博文(51)

文章存档

2012年(11)

2011年(40)

分类:

2011-12-20 16:18:40

原文地址:正则表达式 作者:itanger

只能输入数字:“^[0-9]*$”
只能输入n位的数字:“^\d{n}$”
只能输入至少n位数字:“^\d{n,}$”
只能输入m-n位的数字:“^\d{m,n}$”
只能输入零和非零开头的数字:“^(0|[1-9][0-9]*)$”
只能输入有两位小数的正实数:“^[0-9]+(.[0-9]{2})?$”
只能输入有1-3位小数的正实数:“^[0-9]+(.[0-9]{1,3})?$”
只能输入非零的正整数:“^\+?[1-9][0-9]*$”
只能输入非零的负整数:“^\-[1-9][0-9]*$”
只能输入长度为3的字符:“^.{3}$”
只能输入由26个英文字母组成的字符串:“^[A-Za-z]+$”
只能输入由26个大写英文字母组成的字符串:“^[A-Z]+$”
只能输入由26个小写英文字母组成的字符串:“^[a-z]+$”
只能输入由数字和26个英文字母组成的字符串:“^[A-Za-z0-9]+$”
只能输入由数字、26个英文字母或者下划线组成的字符串:“^\w+$”
验证用户密码:“^[a-zA-Z]\w{5,17}$”正确格式为:以字母开头,长度在6-18之间,
只能包含字符、数字和下划线。
验证是否含有^%&',;=?$\"等字符:“[^%&',;=?$\x22]+”
只能输入汉字:“^[\u4e00-\u9fa5],{0,}$”
验证Email地址:“^\w+[-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$”
验证InternetURL:“^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$”
验证IP地址:(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?
验证电话号码:“^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$”
正确格式为:“XXXX-XXXXXXX”,“XXXX-XXXXXXXX”,“XXX-XXXXXXX”,
“XXX-XXXXXXXX”,“XXXXXXX”,“XXXXXXXX”。
验证身份证号(15位或18位数字):“^\d{15}|\d{}18$”
验证一年的12个月:“^(0?[1-9]|1[0-2])$”正确格式为:“01”-“09”和“1”“12”
验证一个月的31天:“^((0?[1-9])|((1|2)[0-9])|30|31)$”
正确格式为:“01”“09”和“1”“31”。
 
=========================================================================================================
///   
        /// 校验字符串是否只包含字母与数字  
        ///
  
        /// 需要校验的字符串  
        /// true表示符合要求,false表示不符合要求  
        public static bool IsOnlyLetterAndDigit(string toVerified)  
        {  
            Regex rx = new Regex(@"^[a-zA-Z0-9-]*$");  
            return rx.IsMatch(toVerified.Trim(), 0);  
        }  
 
        ///   
        /// 检验是否是整数  
        ///
  
        /// 需要检验的字符串  
        /// 是否为整数:true是整数,false非整数  
        public static bool IsInt(string str)  
        {  
            Regex rx = new Regex(@"^[0123456789]+$");  
            return rx.IsMatch(str);  
        }  
 
        ///   
        /// 校验是否为正的浮点数  
        ///
  
        /// 需要检验的字符串  
        /// 是否为正浮点,是返回true,否则返回false  
        public static bool IsFloat(string str)  
        {  
            Regex rx = new Regex(@"^[0-9]*(.)?[0-9]+$", RegexOptions.IgnoreCase);  
            return rx.IsMatch(str.Trim());    
        }  
 
        ///   
        /// 检验是否为数字  
        ///
  
        /// 需要检验的字符串  
        /// 是否为数字:true代表是,false代表否  
        public static bool IsNumber(string str)  
        {  
 
            Regex rx = new Regex(@"^[+-]?[0123456789]*[.]?[0123456789]*$");  
            return rx.IsMatch(str);  
        }  
 
        ///   
        /// 检验字符串是否为日期时间  
        ///
  
        /// 需要检验的字符串  
        /// 是否为日期时间:true代表是,false代表否  
        public static bool IsDateTime(string str)  
        {  
           // Regex rx = new Regex(@"^[ ]*[012 ]?[0123456789]?[0123456789]{2}[ ]*[-]{1}[ ]*[01]?[0123456789]{1}[ ]*[-]{1}[ ]*[0123]?[0123456789]{1}[ ]*[012]?[0123456789]{1}[ ]*[:]{1}[ ]*[012345]?[0123456789]{1}[ ]*[:]{1}[ ]*[012345]?[0123456789]{1}[ ]*$");  
   Regex rx = new    Regex(@"(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)")
   //Regex(@"[1-2]{1}[0-9]{3}((-|\/){1}(([0]?[1-9]{1})|(1[0-2]{1}))((-|\/){1}((([0]?[1-9]{1})|([1-2]{1}[0-9]{1})|(3[0-1]{1})))( (([0-1]{1}[0-9]{1})|2[0-3]{1}):([0-5]{1}[0-9]{1}):([0-5]{1}[0-9]{1})(\.[0-9]{3})?)?)?)?$");
            return rx.IsMatch(str);  
        }  
 
        ///   
        /// 检验字符串是否为邮政编码  
        ///
  
        /// 需要检验的字符串  
        /// 是否为邮政编码:true代表是,false代表否  
        public static bool IsPostCode(string str)  
        {  
            Regex rx = new Regex(@"^[0123456789]{6}$");  
            return rx.IsMatch(str);  
        }  
 
        ///   
        /// 检验字符串是否为身份证号  
        ///
  
        /// 需要检验的字符串  
        /// 是否为身份证号:true代表是,false代表否  
        public static bool IsCode(string str)  
        {  
            Regex rx = new Regex(@"^[0123456789]{15,18}$");  
            return rx.IsMatch(str);  
        }  
 
        ///   
        /// 检验字符串是否为电子邮件  
        ///
  
        /// 需要检验的字符串  
        /// 是否为电子邮件:true代表是,false代表否  
        public static bool IsEMail(string str)  
        {  
            Regex rx = new Regex(@"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*");  
            return rx.IsMatch(str);  
        }  
 
        ///   
        /// 检验字符串是否为中国地区的电话号码  
        ///
  
        /// 需要检验的字符串  
        /// 是否为中国地区的电话号码:true代表是,false代表否  
        public static bool IsPhoneNumber(string str)  
        {  
            Regex rx = new Regex(@"((d{3,4})|d{3,4}-)?d{7,8}(-d{3})*");  
            return rx.IsMatch(str);  
        }  
 
        ///   
        /// 检验字符串是否为汉字  
        ///
  
        /// 需要检验的字符串  
        /// 是否为汉字:true代表是,false代表否  
        public static bool IsChinese(string str)  
        {  
            Regex rx = new Regex(@"u4e00-u9fa5");  
            return rx.IsMatch(str);  
        }  
 
        ///   
        /// 检验字符串是否为双字节字符(包括汉字)  
        ///
  
        /// 需要检验的字符串  
        /// 是否为双字节字符:true代表是,false代表否  
        public static bool IsDoubleByteChar(string str)  
        {  
            Regex rx = new Regex(@"[^x00-xff]");  
            return rx.IsMatch(str);  
        }  
 
        ///   
        /// 检验字符串是否为URL地址  
        ///
  
        /// 需要检验的字符串  
        /// 是否为URL地址:true代表是,false代表否  
        public static bool IsURLAddress(string str)  
        {  
            Regex rx = new Regex(@"[a-zA-z]+://[^s]*");  
            return rx.IsMatch(str);  
        }  
 
        ///   
        /// 检验字符串是否为IP地址  
        ///
  
        /// 需要检验的字符串  
        /// 是否为IP地址:true代表是,false代表否  
        public static bool IsIPAddress(string str)  
        {  
            Regex rx = new Regex(@"d+.d+.d+.d+");  
            return rx.IsMatch(str);  
        }  
 
        ///   
        /// 清除字符串中的HTML标签(对于复杂的嵌套标签有时不准确)  
        ///
  
        /// 指定的要被处理的字符串  
        /// 清除HTML标签后的字符串  
        public static string RemoveHtmlTags(string toEvaluate)  
        {  
            Regex rx = new Regex(@"s/<[a-zA-Z/][^>]*>//g", RegexOptions.IgnoreCase);  
 
            return rx.Replace(toEvaluate, "");  
        }  

 
阅读(775) | 评论(0) | 转发(0) |
0

上一篇:In语集锦

下一篇:正则表达式详解

给主人留下些什么吧!~~