Regex,簡單的常用的運算式。

來源:互聯網
上載者:User
  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   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.