常用正则表达式,已封装为静态方法,

#region Regular Expression
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 校验字符串是否只包含字母与数字
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="toVerified">需要校验的字符串</param>
ExpandedSubBlockEnd.gif       
/// <returns>true表示符合要求,false表示不符合要求</returns>

InBlock.gif        public static bool IsOnlyLetterAndDigit(string toVerified)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
...{
InBlock.gif            Regex rx
= new Regex(@"^[a-zA-Z0-9-]*$");
InBlock.gif           
return rx.IsMatch(toVerified.Trim(), 0);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 检验是否是整数
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="str">需要检验的字符串</param>
ExpandedSubBlockEnd.gif       
/// <returns>是否为整数:true是整数,false非整数</returns>

InBlock.gif        public static bool IsInt(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
...{
InBlock.gif            Regex rx
= new Regex(@"^[0123456789]+$");
InBlock.gif           
return rx.IsMatch(str);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 校验是否为正的浮点数
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="price">需要检验的字符串</param>
ExpandedSubBlockEnd.gif       
/// <returns>是否为正浮点,是返回true,否则返回false</returns>

InBlock.gif        public static bool IsFloat(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
...{
InBlock.gif            Regex rx
= new Regex(@"^[0-9]*(.)?[0-9]+$", RegexOptions.IgnoreCase);
InBlock.gif           
return rx.IsMatch(str.Trim());   
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 检验是否为数字
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="str">需要检验的字符串</param>
ExpandedSubBlockEnd.gif       
/// <returns>是否为数字:true代表是,false代表否</returns>

InBlock.gif        public static bool IsNumber(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
...{
InBlock.gif
InBlock.gif            Regex rx
= new Regex(@"^[+-]?[0123456789]*[.]?[0123456789]*$");
InBlock.gif           
return rx.IsMatch(str);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 检验字符串是否为日期时间
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="str">需要检验的字符串</param>
ExpandedSubBlockEnd.gif       
/// <returns>是否为日期时间:true代表是,false代表否</returns>

InBlock.gif        public static bool IsDateTime(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
...{
InBlock.gif            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}[ ]*$");
InBlock.gif           
return rx.IsMatch(str);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 检验字符串是否为邮政编码
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="str">需要检验的字符串</param>
ExpandedSubBlockEnd.gif       
/// <returns>是否为邮政编码:true代表是,false代表否</returns>

InBlock.gif        public static bool IsPostCode(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
...{
InBlock.gif            Regex rx
= new Regex(@"^[0123456789]{6}$");
InBlock.gif           
return rx.IsMatch(str);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 检验字符串是否为身份证号
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="str">需要检验的字符串</param>
ExpandedSubBlockEnd.gif       
/// <returns>是否为身份证号:true代表是,false代表否</returns>

InBlock.gif        public static bool IsCode(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
...{
InBlock.gif            Regex rx
= new Regex(@"^[0123456789]{15,18}$");
InBlock.gif           
return rx.IsMatch(str);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 检验字符串是否为电子邮件
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="str">需要检验的字符串</param>
ExpandedSubBlockEnd.gif       
/// <returns>是否为电子邮件:true代表是,false代表否</returns>

InBlock.gif        public static bool IsEMail(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
...{
InBlock.gif            Regex rx
= new Regex(@"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*");
InBlock.gif           
return rx.IsMatch(str);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 检验字符串是否为中国地区的电话号码
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="str">需要检验的字符串</param>
ExpandedSubBlockEnd.gif       
/// <returns>是否为中国地区的电话号码:true代表是,false代表否</returns>

InBlock.gif        public static bool IsPhoneNumber(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
...{
InBlock.gif            Regex rx
= new Regex(@"((d{3,4})|d{3,4}-)?d{7,8}(-d{3})*");
InBlock.gif           
return rx.IsMatch(str);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 检验字符串是否为汉字
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="str">需要检验的字符串</param>
ExpandedSubBlockEnd.gif       
/// <returns>是否为汉字:true代表是,false代表否</returns>

InBlock.gif        public static bool IsChinese(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
...{
InBlock.gif            Regex rx
= new Regex(@"u4e00-u9fa5");
InBlock.gif           
return rx.IsMatch(str);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 检验字符串是否为双字节字符(包括汉字)
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="str">需要检验的字符串</param>
ExpandedSubBlockEnd.gif       
/// <returns>是否为双字节字符:true代表是,false代表否</returns>

InBlock.gif        public static bool IsDoubleByteChar(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
...{
InBlock.gif            Regex rx
= new Regex(@"[^x00-xff]");
InBlock.gif           
return rx.IsMatch(str);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 检验字符串是否为URL地址
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="str">需要检验的字符串</param>
ExpandedSubBlockEnd.gif       
/// <returns>是否为URL地址:true代表是,false代表否</returns>

InBlock.gif        public static bool IsURLAddress(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
...{
InBlock.gif            Regex rx
= new Regex(@"[a-zA-z]+://[^s]*");
InBlock.gif           
return rx.IsMatch(str);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 检验字符串是否为IP地址
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="str">需要检验的字符串</param>
ExpandedSubBlockEnd.gif       
/// <returns>是否为IP地址:true代表是,false代表否</returns>

InBlock.gif        public static bool IsIPAddress(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
...{
InBlock.gif            Regex rx
= new Regex(@"d+.d+.d+.d+");
InBlock.gif           
return rx.IsMatch(str);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif       
/**//// <summary>
InBlock.gif       
/// 清除字符串中的HTML标签(对于复杂的嵌套标签有时不准确)
InBlock.gif       
/// </summary>
InBlock.gif       
/// <param name="toEvaluate">指定的要被处理的字符串</param>
ExpandedSubBlockEnd.gif       
/// <returns>清除HTML标签后的字符串</returns>

InBlock.gif        public static string RemoveHtmlTags(string toEvaluate)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
...{
InBlock.gif            Regex rx
= new Regex(@"s/<[a-zA-Z/][^>]*>//g", RegexOptions.IgnoreCase);
InBlock.gif
InBlock.gif           
return rx.Replace(toEvaluate, "");
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif       
#endregion

转载于:https://www.cnblogs.com/pbwf/archive/2007/05/26/761114.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值