正则表达式,简单的常用的表达式。

  1. #region Regular Expression   
  2.   
  3.         /**//// <SUMMARY>   
  4.         /// 校验字符串是否只包含字母与数字   
  5.         /// </SUMMARY>   
  6.         /// <PARAM name="toVerified">需要校验的字符串</PARAM>   
  7.         /// <RETURNS>true表示符合要求,false表示不符合要求</RETURNS>   
  8.         public static bool IsOnlyLetterAndDigit(string toVerified)   
  9.         ...{   
  10.             Regex rx = new Regex(@"^[a-zA-Z0-9-]*$");   
  11.             return rx.IsMatch(toVerified.Trim(), 0);   
  12.         }   
  13.   
  14.         /**//// <SUMMARY>   
  15.         /// 检验是否是整数   
  16.         /// </SUMMARY>   
  17.         /// <PARAM name="str">需要检验的字符串</PARAM>   
  18.         /// <RETURNS>是否为整数:true是整数,false非整数</RETURNS>   
  19.         public static bool IsInt(string str)   
  20.         ...{   
  21.             Regex rx = new Regex(@"^[0123456789]+$");   
  22.             return rx.IsMatch(str);   
  23.         }   
  24.   
  25.         /**//// <SUMMARY>   
  26.         /// 校验是否为正的浮点数   
  27.         /// </SUMMARY>   
  28.         /// <PARAM name="price">需要检验的字符串</PARAM>   
  29.         /// <RETURNS>是否为正浮点,是返回true,否则返回false</RETURNS>   
  30.         public static bool IsFloat(string str)   
  31.         ...{   
  32.             Regex rx = new Regex(@"^[0-9]*(.)?[0-9]+$", RegexOptions.IgnoreCase);   
  33.             return rx.IsMatch(str.Trim());       
  34.         }   
  35.   
  36.         /**//// <SUMMARY>   
  37.         /// 检验是否为数字   
  38.         /// </SUMMARY>   
  39.         /// <PARAM name="str">需要检验的字符串</PARAM>   
  40.         /// <RETURNS>是否为数字:true代表是,false代表否</RETURNS>   
  41.         public static bool IsNumber(string str)   
  42.         ...{   
  43.   
  44.             Regex rx = new Regex(@"^[+-]?[0123456789]*[.]?[0123456789]*$");   
  45.             return rx.IsMatch(str);   
  46.         }   
  47.   
  48.         /**//// <SUMMARY>   
  49.         /// 检验字符串是否为日期时间   
  50.         /// </SUMMARY>   
  51.         /// <PARAM name="str">需要检验的字符串</PARAM>   
  52.         /// <RETURNS>是否为日期时间:true代表是,false代表否</RETURNS>   
  53.         public static bool IsDateTime(string str)   
  54.         ...{   
  55.             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}[ ]*$");   
  56.             return rx.IsMatch(str);   
  57.         }   
  58.   
  59.         /**//// <SUMMARY>   
  60.         /// 检验字符串是否为邮政编码   
  61.         /// </SUMMARY>   
  62.         /// <PARAM name="str">需要检验的字符串</PARAM>   
  63.         /// <RETURNS>是否为邮政编码:true代表是,false代表否</RETURNS>   
  64.         public static bool IsPostCode(string str)   
  65.         ...{   
  66.             Regex rx = new Regex(@"^[0123456789]{6}$");   
  67.             return rx.IsMatch(str);   
  68.         }   
  69.   
  70.         /**//// <SUMMARY>   
  71.         /// 检验字符串是否为身份证号   
  72.         /// </SUMMARY>   
  73.         /// <PARAM name="str">需要检验的字符串</PARAM>   
  74.         /// <RETURNS>是否为身份证号:true代表是,false代表否</RETURNS>   
  75.         public static bool IsCode(string str)   
  76.         ...{   
  77.             Regex rx = new Regex(@"^[0123456789]{15,18}$");   
  78.             return rx.IsMatch(str);   
  79.         }   
  80.   
  81.         /**//// <SUMMARY>   
  82.         /// 检验字符串是否为电子邮件   
  83.         /// </SUMMARY>   
  84.         /// <PARAM name="str">需要检验的字符串</PARAM>   
  85.         /// <RETURNS>是否为电子邮件:true代表是,false代表否</RETURNS>   
  86.         public static bool IsEMail(string str)   
  87.         ...{   
  88.             Regex rx = new Regex(@"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*");   
  89.             return rx.IsMatch(str);   
  90.         }   
  91.   
  92.         /**//// <SUMMARY>   
  93.         /// 检验字符串是否为中国地区的电话号码   
  94.         /// </SUMMARY>   
  95.         /// <PARAM name="str">需要检验的字符串</PARAM>   
  96.         /// <RETURNS>是否为中国地区的电话号码:true代表是,false代表否</RETURNS>   
  97.         public static bool IsPhoneNumber(string str)   
  98.         ...{   
  99.             Regex rx = new Regex(@"((d{3,4})|d{3,4}-)?d{7,8}(-d{3})*");   
  100.             return rx.IsMatch(str);   
  101.         }   
  102.   
  103.         /**//// <SUMMARY>   
  104.         /// 检验字符串是否为汉字   
  105.         /// </SUMMARY>   
  106.         /// <PARAM name="str">需要检验的字符串</PARAM>   
  107.         /// <RETURNS>是否为汉字:true代表是,false代表否</RETURNS>   
  108.         public static bool IsChinese(string str)   
  109.         ...{   
  110.             Regex rx = new Regex(@"u4e00-u9fa5");   
  111.             return rx.IsMatch(str);   
  112.         }   
  113.   
  114.         /**//// <SUMMARY>   
  115.         /// 检验字符串是否为双字节字符(包括汉字)   
  116.         /// </SUMMARY>   
  117.         /// <PARAM name="str">需要检验的字符串</PARAM>   
  118.         /// <RETURNS>是否为双字节字符:true代表是,false代表否</RETURNS>   
  119.         public static bool IsDoubleByteChar(string str)   
  120.         ...{   
  121.             Regex rx = new Regex(@"[^x00-xff]");   
  122.             return rx.IsMatch(str);   
  123.         }   
  124.   
  125.         /**//// <SUMMARY>   
  126.         /// 检验字符串是否为URL地址   
  127.         /// </SUMMARY>   
  128.         /// <PARAM name="str">需要检验的字符串</PARAM>   
  129.         /// <RETURNS>是否为URL地址:true代表是,false代表否</RETURNS>   
  130.         public static bool IsURLAddress(string str)   
  131.         ...{   
  132.             Regex rx = new Regex(@"[a-zA-z]+://[^s]*");   
  133.             return rx.IsMatch(str);   
  134.         }   
  135.   
  136.         /**//// <SUMMARY>   
  137.         /// 检验字符串是否为IP地址   
  138.         /// </SUMMARY>   
  139.         /// <PARAM name="str">需要检验的字符串</PARAM>   
  140.         /// <RETURNS>是否为IP地址:true代表是,false代表否</RETURNS>   
  141.         public static bool IsIPAddress(string str)   
  142.         ...{   
  143.             Regex rx = new Regex(@"d+.d+.d+.d+");   
  144.             return rx.IsMatch(str);   
  145.         }   
  146.   
  147.         /**//// <SUMMARY>   
  148.         /// 清除字符串中的HTML标签(对于复杂的嵌套标签有时不准确)   
  149.         /// </SUMMARY>   
  150.         /// <PARAM name="toEvaluate">指定的要被处理的字符串</PARAM>   
  151.         /// <RETURNS>清除HTML标签后的字符串</RETURNS>   
  152.         public static string RemoveHtmlTags(string toEvaluate)   
  153.         ...{   
  154.             Regex rx = new Regex(@"s/<[a-zA-Z/][^>]*>//g", RegexOptions.IgnoreCase);   
  155.   
  156.             return rx.Replace(toEvaluate, "");   
  157.         }   
  158.  
  159.         #endregion   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值